From 03152ea0145de17912c035874b180125b7f353cf Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Mon, 24 Jan 2022 16:32:41 -0500 Subject: [PATCH] Include trouble-shooting URLs in errors (#2489). --- packages/bignumber/src.ts/bignumber.ts | 18 ++++++------ packages/logger/src.ts/index.ts | 39 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/packages/bignumber/src.ts/bignumber.ts b/packages/bignumber/src.ts/bignumber.ts index 656018909..872661823 100644 --- a/packages/bignumber/src.ts/bignumber.ts +++ b/packages/bignumber/src.ts/bignumber.ts @@ -83,7 +83,7 @@ export class BigNumber implements Hexable { div(other: BigNumberish): BigNumber { const o = BigNumber.from(other); if (o.isZero()) { - throwFault("division by zero", "div"); + throwFault("division-by-zero", "div"); } return toBigNumber(toBN(this).div(toBN(other))); } @@ -95,7 +95,7 @@ export class BigNumber implements Hexable { mod(other: BigNumberish): BigNumber { const value = toBN(other); if (value.isNeg()) { - throwFault("cannot modulo negative values", "mod"); + throwFault("division-by-zero", "mod"); } return toBigNumber(toBN(this).umod(value)); } @@ -103,7 +103,7 @@ export class BigNumber implements Hexable { pow(other: BigNumberish): BigNumber { const value = toBN(other); if (value.isNeg()) { - throwFault("cannot raise to negative values", "pow"); + throwFault("negative-power", "pow"); } return toBigNumber(toBN(this).pow(value)); } @@ -111,7 +111,7 @@ export class BigNumber implements Hexable { and(other: BigNumberish): BigNumber { const value = toBN(other); if (this.isNegative() || value.isNeg()) { - throwFault("cannot 'and' negative values", "and"); + throwFault("unbound-bitwise-result", "and"); } return toBigNumber(toBN(this).and(value)); } @@ -119,7 +119,7 @@ export class BigNumber implements Hexable { or(other: BigNumberish): BigNumber { const value = toBN(other); if (this.isNegative() || value.isNeg()) { - throwFault("cannot 'or' negative values", "or"); + throwFault("unbound-bitwise-result", "or"); } return toBigNumber(toBN(this).or(value)); } @@ -127,28 +127,28 @@ export class BigNumber implements Hexable { xor(other: BigNumberish): BigNumber { const value = toBN(other); if (this.isNegative() || value.isNeg()) { - throwFault("cannot 'xor' negative values", "xor"); + throwFault("unbound-bitwise-result", "xor"); } return toBigNumber(toBN(this).xor(value)); } mask(value: number): BigNumber { if (this.isNegative() || value < 0) { - throwFault("cannot mask negative values", "mask"); + throwFault("negative-width", "mask"); } return toBigNumber(toBN(this).maskn(value)); } shl(value: number): BigNumber { if (this.isNegative() || value < 0) { - throwFault("cannot shift negative values", "shl"); + throwFault("negative-width", "shl"); } return toBigNumber(toBN(this).shln(value)); } shr(value: number): BigNumber { if (this.isNegative() || value < 0) { - throwFault("cannot shift negative values", "shr"); + throwFault("negative-width", "shr"); } return toBigNumber(toBN(this).shrn(value)); } diff --git a/packages/logger/src.ts/index.ts b/packages/logger/src.ts/index.ts index 87808ec61..c568f034f 100644 --- a/packages/logger/src.ts/index.ts +++ b/packages/logger/src.ts/index.ts @@ -217,6 +217,45 @@ export class Logger { messageDetails.push(`version=${ this.version }`); const reason = message; + + let url = ""; + + switch (code) { + case ErrorCode.NUMERIC_FAULT: { + url = "NUMERIC_FAULT"; + const fault = message; + + switch (fault) { + case "overflow": case "underflow": + url += "-" + fault; + break; + case "division-by-zero": case "negative-modulo": + url += "-undefined"; + break; + case "negative-power": case "negative-width": + url += "-unsupported"; + break; + case "unbound-bitwise-result": + url += "-unbound-result"; + break; + } + break; + } + case ErrorCode.CALL_EXCEPTION: + case ErrorCode.INSUFFICIENT_FUNDS: + case ErrorCode.MISSING_NEW: + case ErrorCode.NONCE_EXPIRED: + case ErrorCode.REPLACEMENT_UNDERPRICED: + case ErrorCode.TRANSACTION_REPLACED: + case ErrorCode.UNPREDICTABLE_GAS_LIMIT: + url = code; + break; + } + + if (url) { + message += " [ See: https:/\/ethers.org/errors/" + url + " ]"; + } + if (messageDetails.length) { message += " (" + messageDetails.join(", ") + ")"; }