aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/decorators.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-06-11 19:41:28 +0800
committerGitHub <noreply@github.com>2017-06-11 19:41:28 +0800
commit4e56c299263cf6a3397a1d7b95fb92a8b61da3c0 (patch)
treeec2ed737cb24c4d4883d1b2253fc4e35c25ec9df /src/utils/decorators.ts
parent7838e1964f112b8df63422c23a675a7946c7b9e3 (diff)
parent88de98080cda4933ed8b15246c59891aa182d31e (diff)
downloaddexon-sol-tools-4e56c299263cf6a3397a1d7b95fb92a8b61da3c0.tar
dexon-sol-tools-4e56c299263cf6a3397a1d7b95fb92a8b61da3c0.tar.gz
dexon-sol-tools-4e56c299263cf6a3397a1d7b95fb92a8b61da3c0.tar.bz2
dexon-sol-tools-4e56c299263cf6a3397a1d7b95fb92a8b61da3c0.tar.lz
dexon-sol-tools-4e56c299263cf6a3397a1d7b95fb92a8b61da3c0.tar.xz
dexon-sol-tools-4e56c299263cf6a3397a1d7b95fb92a8b61da3c0.tar.zst
dexon-sol-tools-4e56c299263cf6a3397a1d7b95fb92a8b61da3c0.zip
Merge pull request #60 from 0xProject/error-decorator
Add initial error handling decorator implementation
Diffstat (limited to 'src/utils/decorators.ts')
-rw-r--r--src/utils/decorators.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/utils/decorators.ts b/src/utils/decorators.ts
new file mode 100644
index 000000000..a25f2cff5
--- /dev/null
+++ b/src/utils/decorators.ts
@@ -0,0 +1,35 @@
+import * as _ from 'lodash';
+import {constants} from './constants';
+import {AsyncMethod, ZeroExError} from '../types';
+
+export const decorators = {
+ /**
+ * Source: https://stackoverflow.com/a/29837695/3546986
+ */
+ contractCallErrorHandler(target: object,
+ key: string|symbol,
+ descriptor: TypedPropertyDescriptor<AsyncMethod>,
+ ): TypedPropertyDescriptor<AsyncMethod> {
+ const originalMethod = (descriptor.value as AsyncMethod);
+
+ // Do not use arrow syntax here. Use a function expression in
+ // order to use the correct value of `this` in this method
+ // tslint:disable-next-line:only-arrow-functions
+ descriptor.value = async function(...args: any[]) {
+ try {
+ const result = await originalMethod.apply(this, args);
+ return result;
+ } catch (error) {
+ if (_.includes(error.message, constants.INVALID_JUMP_PATTERN)) {
+ throw new Error(ZeroExError.INVALID_JUMP);
+ }
+ if (_.includes(error.message, constants.OUT_OF_GAS_PATTERN)) {
+ throw new Error(ZeroExError.OUT_OF_GAS);
+ }
+ throw error;
+ }
+ };
+
+ return descriptor;
+ },
+};