aboutsummaryrefslogtreecommitdiffstats
path: root/packages/0x.js/src/utils/decorators.ts
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-11-13 11:17:18 +0800
committerFabio Berger <me@fabioberger.com>2017-11-13 11:17:18 +0800
commitc4ee2d73865a1444c079b9e2836b7630a0adf03e (patch)
treeb9c7794e7022fb189675d914f5fe58dcabd67dec /packages/0x.js/src/utils/decorators.ts
parenta74ec0effa818a86233fe64cb0dad2c61bbb4bb6 (diff)
downloaddexon-0x-contracts-c4ee2d73865a1444c079b9e2836b7630a0adf03e.tar
dexon-0x-contracts-c4ee2d73865a1444c079b9e2836b7630a0adf03e.tar.gz
dexon-0x-contracts-c4ee2d73865a1444c079b9e2836b7630a0adf03e.tar.bz2
dexon-0x-contracts-c4ee2d73865a1444c079b9e2836b7630a0adf03e.tar.lz
dexon-0x-contracts-c4ee2d73865a1444c079b9e2836b7630a0adf03e.tar.xz
dexon-0x-contracts-c4ee2d73865a1444c079b9e2836b7630a0adf03e.tar.zst
dexon-0x-contracts-c4ee2d73865a1444c079b9e2836b7630a0adf03e.zip
Switch over to Lerna + Yarn Workspaces setup for a mono-repo approach
Diffstat (limited to 'packages/0x.js/src/utils/decorators.ts')
-rw-r--r--packages/0x.js/src/utils/decorators.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/packages/0x.js/src/utils/decorators.ts b/packages/0x.js/src/utils/decorators.ts
new file mode 100644
index 000000000..ec750b891
--- /dev/null
+++ b/packages/0x.js/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.InvalidJump);
+ }
+ if (_.includes(error.message, constants.OUT_OF_GAS_PATTERN)) {
+ throw new Error(ZeroExError.OutOfGas);
+ }
+ throw error;
+ }
+ };
+
+ return descriptor;
+ },
+};