aboutsummaryrefslogtreecommitdiffstats
path: root/packages/0x.js/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'packages/0x.js/src/utils')
-rw-r--r--packages/0x.js/src/utils/constants.ts1
-rw-r--r--packages/0x.js/src/utils/decorators.ts84
2 files changed, 68 insertions, 17 deletions
diff --git a/packages/0x.js/src/utils/constants.ts b/packages/0x.js/src/utils/constants.ts
index 3de3f5bc1..6001e1c7f 100644
--- a/packages/0x.js/src/utils/constants.ts
+++ b/packages/0x.js/src/utils/constants.ts
@@ -6,6 +6,7 @@ export const constants = {
MAX_DIGITS_IN_UNSIGNED_256_INT: 78,
INVALID_JUMP_PATTERN: 'invalid JUMP at',
OUT_OF_GAS_PATTERN: 'out of gas',
+ INVALID_TAKER_FORMAT: 'instance.taker is not of a type(s) string',
UNLIMITED_ALLOWANCE_IN_BASE_UNITS: new BigNumber(2).pow(256).minus(1),
DEFAULT_BLOCK_POLLING_INTERVAL: 1000,
};
diff --git a/packages/0x.js/src/utils/decorators.ts b/packages/0x.js/src/utils/decorators.ts
index 1760d8872..060a26df9 100644
--- a/packages/0x.js/src/utils/decorators.ts
+++ b/packages/0x.js/src/utils/decorators.ts
@@ -1,17 +1,37 @@
import * as _ from 'lodash';
-import {AsyncMethod, ZeroExError} from '../types';
+import {AsyncMethod, SyncMethod, ZeroExError} from '../types';
import {constants} from './constants';
-export const decorators = {
- /**
- * Source: https://stackoverflow.com/a/29837695/3546986
- */
- contractCallErrorHandler(target: object,
- key: string|symbol,
- descriptor: TypedPropertyDescriptor<AsyncMethod>,
- ): TypedPropertyDescriptor<AsyncMethod> {
+type ErrorTransformer = (err: Error) => Error;
+
+const contractCallErrorTransformer = (error: Error) => {
+ if (_.includes(error.message, constants.INVALID_JUMP_PATTERN)) {
+ return new Error(ZeroExError.InvalidJump);
+ }
+ if (_.includes(error.message, constants.OUT_OF_GAS_PATTERN)) {
+ return new Error(ZeroExError.OutOfGas);
+ }
+ return error;
+};
+
+const schemaErrorTransformer = (error: Error) => {
+ if (_.includes(error.message, constants.INVALID_TAKER_FORMAT)) {
+ // tslint:disable-next-line:max-line-length
+ const errMsg = 'Order taker must be of type string. If you want anyone to be able to fill an order - pass ZeroEx.NULL_ADDRESS';
+ throw new Error(errMsg);
+ }
+ return error;
+};
+
+/**
+ * Source: https://stackoverflow.com/a/29837695/3546986
+ */
+const asyncErrorHandlerFactory = (errorTransformer: ErrorTransformer) => {
+ const asyncErrorHandlingDecorator = (
+ target: object, key: string|symbol, descriptor: TypedPropertyDescriptor<AsyncMethod>,
+ ) => {
const originalMethod = (descriptor.value as AsyncMethod);
// Do not use arrow syntax here. Use a function expression in
@@ -22,16 +42,46 @@ export const decorators = {
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;
+ const transformedError = errorTransformer(error);
+ throw transformedError;
}
};
return descriptor;
- },
+ };
+
+ return asyncErrorHandlingDecorator;
+};
+
+const syncErrorHandlerFactory = (errorTransformer: ErrorTransformer) => {
+ const syncErrorHandlingDecorator = (
+ target: object, key: string|symbol, descriptor: TypedPropertyDescriptor<SyncMethod>,
+ ) => {
+ const originalMethod = (descriptor.value as SyncMethod);
+
+ // 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 = function(...args: any[]) {
+ try {
+ const result = originalMethod.apply(this, args);
+ return result;
+ } catch (error) {
+ const transformedError = errorTransformer(error);
+ throw transformedError;
+ }
+ };
+
+ return descriptor;
+ };
+
+ return syncErrorHandlingDecorator;
+};
+
+// _.flow(f, g) = f ∘ g
+const zeroExErrorTransformer = _.flow(schemaErrorTransformer, contractCallErrorTransformer);
+
+export const decorators = {
+ asyncZeroExErrorHandler: asyncErrorHandlerFactory(zeroExErrorTransformer),
+ syncZeroExErrorHandler: syncErrorHandlerFactory(zeroExErrorTransformer),
};