Merge pull request #60 from 0xProject/error-decorator

Add initial error handling decorator implementation
This commit is contained in:
Fabio Berger
2017-06-11 13:41:28 +02:00
committed by GitHub
5 changed files with 50 additions and 0 deletions

View File

@@ -35,6 +35,7 @@ import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_reque
import {signedOrderSchema, orderSchema} from '../schemas/order_schemas';
import {constants} from '../utils/constants';
import {TokenWrapper} from './token_wrapper';
import {decorators} from '../utils/decorators';
export class ExchangeWrapper extends ContractWrapper {
private exchangeContractErrCodesToMsg = {
@@ -135,6 +136,7 @@ export class ExchangeWrapper extends ContractWrapper {
* @param takerAddress The user Ethereum address who would like to fill this order.
* Must be available via the supplied Web3.Provider passed to 0x.js.
*/
@decorators.contractCallErrorHandler
public async fillOrderAsync(signedOrder: SignedOrder, takerTokenFillAmount: BigNumber.BigNumber,
shouldCheckTransfer: boolean, takerAddress: string): Promise<void> {
assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema);
@@ -188,6 +190,7 @@ export class ExchangeWrapper extends ContractWrapper {
* @param takerAddress The user Ethereum address who would like to fill these orders.
* Must be available via the supplied Web3.Provider passed to 0x.js.
*/
@decorators.contractCallErrorHandler
public async fillOrdersUpToAsync(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber.BigNumber,
shouldCheckTransfer: boolean, takerAddress: string): Promise<void> {
const takerTokenAddresses = _.map(signedOrders, signedOrder => signedOrder.takerTokenAddress);
@@ -259,6 +262,7 @@ export class ExchangeWrapper extends ContractWrapper {
* @param takerAddress The user Ethereum address who would like to fill these orders.
* Must be available via the supplied Web3.Provider passed to 0x.js.
*/
@decorators.contractCallErrorHandler
public async batchFillOrderAsync(orderFillRequests: OrderFillRequest[],
shouldCheckTransfer: boolean, takerAddress: string): Promise<void> {
assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer);
@@ -323,6 +327,7 @@ export class ExchangeWrapper extends ContractWrapper {
* @param takerAddress The user Ethereum address who would like to fill this order.
* Must be available via the supplied Web3.Provider passed to 0x.js.
*/
@decorators.contractCallErrorHandler
public async fillOrKillOrderAsync(signedOrder: SignedOrder, takerTokenFillAmount: BigNumber.BigNumber,
takerAddress: string): Promise<void> {
assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema);
@@ -369,6 +374,7 @@ export class ExchangeWrapper extends ContractWrapper {
* @param takerAddress The user Ethereum address who would like to fill there orders.
* Must be available via the supplied Web3.Provider passed to 0x.js.
*/
@decorators.contractCallErrorHandler
public async batchFillOrKillAsync(orderFillOrKillRequests: OrderFillOrKillRequest[],
takerAddress: string): Promise<void> {
await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper);
@@ -424,6 +430,7 @@ export class ExchangeWrapper extends ContractWrapper {
* The order you would like to cancel.
* @param takerTokenCancelAmount The amount (specified in taker tokens) that you would like to cancel.
*/
@decorators.contractCallErrorHandler
public async cancelOrderAsync(
order: Order|SignedOrder, takerTokenCancelAmount: BigNumber.BigNumber): Promise<void> {
assert.doesConformToSchema('order', order, orderSchema);
@@ -459,6 +466,7 @@ export class ExchangeWrapper extends ContractWrapper {
* @param orderCancellationRequests An array of JS objects that conform to the OrderCancellationRequest
* interface.
*/
@decorators.contractCallErrorHandler
public async batchCancelOrderAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise<void> {
const makers = _.map(orderCancellationRequests, cancellationRequest => cancellationRequest.order.maker);
assert.hasAtMostOneUniqueValue(makers, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH_DISALLOWED);

View File

@@ -18,6 +18,8 @@ export const ZeroExError = strEnum([
'ZRX_NOT_IN_TOKEN_REGISTRY',
'INSUFFICIENT_ALLOWANCE_FOR_TRANSFER',
'INSUFFICIENT_BALANCE_FOR_TRANSFER',
'INVALID_JUMP',
'OUT_OF_GAS',
]);
export type ZeroExError = keyof typeof ZeroExError;
@@ -263,3 +265,5 @@ export interface OrderFillRequest {
signedOrder: SignedOrder;
takerTokenFillAmount: BigNumber.BigNumber;
}
export type AsyncMethod = (...args: any[]) => Promise<any>;

View File

@@ -2,4 +2,6 @@ export const constants = {
NULL_ADDRESS: '0x0000000000000000000000000000000000000000',
TESTRPC_NETWORK_ID: 50,
MAX_DIGITS_IN_UNSIGNED_256_INT: 78,
INVALID_JUMP_PATTERN: 'invalid JUMP at',
OUT_OF_GAS_PATTERN: 'out of gas',
};

35
src/utils/decorators.ts Normal file
View File

@@ -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;
},
};

View File

@@ -7,6 +7,7 @@
"sourceMap": true,
"declaration": true,
"noImplicitAny": true,
"experimentalDecorators": true,
"strictNullChecks": true
},
"include": [