From 0c33aa16a19979dd69d90a153dca173252ed147d Mon Sep 17 00:00:00 2001 From: Lawrence Forman Date: Thu, 16 Apr 2020 03:12:37 -0400 Subject: [PATCH] `@0x/utils`: Add more ZeroEx rich reverts. `@0x/utils: Display revert error payload in stack traces. --- packages/utils/CHANGELOG.json | 4 +++ packages/utils/src/index.ts | 1 + packages/utils/src/revert_error.ts | 4 ++- .../zero-ex/migrate_revert_errors.ts | 24 +++++++++++++++++ .../zero-ex/proxy_revert_errors.ts | 26 ++++++++++++++----- 5 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 packages/utils/src/revert_errors/zero-ex/migrate_revert_errors.ts diff --git a/packages/utils/CHANGELOG.json b/packages/utils/CHANGELOG.json index 1b29cc13e3..5763b663fb 100644 --- a/packages/utils/CHANGELOG.json +++ b/packages/utils/CHANGELOG.json @@ -5,6 +5,10 @@ { "note": "Add `ZeroExRevertErrors`", "pr": 2540 + }, + { + "note": "Print full revert error in stack traces.", + "pr": 2540 } ] }, diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index cfd773bb64..f5d0d4a3a5 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -48,4 +48,5 @@ export const ZeroExRevertErrors = { Common: require('./revert_errors/zero-ex/common_revert_errors'), Proxy: require('./revert_errors/zero-ex/proxy_revert_errors'), SimpleFunctionRegistry: require('./revert_errors/zero-ex/simple_function_registry_revert_errors'), + Migrate: require('./revert_errors/zero-ex/migrate_revert_errors'), }; diff --git a/packages/utils/src/revert_error.ts b/packages/utils/src/revert_error.ts index 329614f83c..7ca1dfcd9b 100644 --- a/packages/utils/src/revert_error.ts +++ b/packages/utils/src/revert_error.ts @@ -113,7 +113,9 @@ export abstract class RevertError extends Error { const instance = new type(); try { const values = decoder(_bytes); - return _.assign(instance, { values }); + _.assign(instance, { values }); + instance.message = instance.toString(); + return instance; } catch (err) { throw new Error( `Bytes ${_bytes} cannot be decoded as a revert error of type ${instance.signature}: ${err.message}`, diff --git a/packages/utils/src/revert_errors/zero-ex/migrate_revert_errors.ts b/packages/utils/src/revert_errors/zero-ex/migrate_revert_errors.ts new file mode 100644 index 0000000000..ea1ed271d8 --- /dev/null +++ b/packages/utils/src/revert_errors/zero-ex/migrate_revert_errors.ts @@ -0,0 +1,24 @@ +import { RevertError } from '../../revert_error'; + +// tslint:disable:max-classes-per-file +export class AlreadyMigratingError extends RevertError { + constructor() { + super('AlreadyMigratingError', 'AlreadyMigratingError()', {}); + } +} + +export class MigrateCallFailedError extends RevertError { + constructor(target?: string, resultData?: string) { + super('MigrateCallFailedError', 'MigrateCallFailedError(address target, bytes resultData)', { + target, + resultData, + }); + } +} + +const types = [AlreadyMigratingError, MigrateCallFailedError]; + +// Register the types we've defined. +for (const type of types) { + RevertError.registerType(type); +} diff --git a/packages/utils/src/revert_errors/zero-ex/proxy_revert_errors.ts b/packages/utils/src/revert_errors/zero-ex/proxy_revert_errors.ts index cabedc3b63..fc5ff2c5a1 100644 --- a/packages/utils/src/revert_errors/zero-ex/proxy_revert_errors.ts +++ b/packages/utils/src/revert_errors/zero-ex/proxy_revert_errors.ts @@ -9,12 +9,6 @@ export class NotImplementedError extends RevertError { } } -export class AlreadyBootstrappedError extends RevertError { - constructor() { - super('AlreadyBootstrappedError', 'AlreadyBootstrappedError()', {}); - } -} - export class InvalidBootstrapCallerError extends RevertError { constructor(caller?: string, expectedCaller?: string) { super('InvalidBootstrapCallerError', 'InvalidBootstrapCallerError(address caller, address expectedCaller)', { @@ -24,7 +18,25 @@ export class InvalidBootstrapCallerError extends RevertError { } } -const types = [AlreadyBootstrappedError, InvalidBootstrapCallerError, NotImplementedError]; +export class InvalidDieCallerError extends RevertError { + constructor(caller?: string, expectedCaller?: string) { + super('InvalidDieCallerError', 'InvalidDieCallerError(address caller, address expectedCaller)', { + caller, + expectedCaller, + }); + } +} + +export class BootstrapCallFailedError extends RevertError { + constructor(target?: string, resultData?: string) { + super('BootstrapCallFailedError', 'BootstrapCallFailedError(address target, bytes resultData)', { + target, + resultData, + }); + } +} + +const types = [BootstrapCallFailedError, InvalidBootstrapCallerError, InvalidDieCallerError, NotImplementedError]; // Register the types we've defined. for (const type of types) {