From f3c6d819f34b6d93f53d98b9f337ade5aa37a594 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Tue, 5 Oct 2021 16:05:40 -0400 Subject: [PATCH] Added more information to some invalid argument errors (#1130). --- packages/contracts/src.ts/index.ts | 7 ++++++- packages/solidity/package.json | 13 +++++++------ packages/solidity/src.ts/index.ts | 23 +++++++++++++++++------ 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/packages/contracts/src.ts/index.ts b/packages/contracts/src.ts/index.ts index 5a041f13c..c43dbfbf1 100644 --- a/packages/contracts/src.ts/index.ts +++ b/packages/contracts/src.ts/index.ts @@ -157,7 +157,12 @@ async function resolveAddresses(resolver: Signer | Provider, value: any, paramTy } if (paramType.baseType === "array") { - if (!Array.isArray(value)) { return Promise.reject(new Error("invalid value for array")); } + if (!Array.isArray(value)) { + return Promise.reject(logger.makeError("invalid value for array", Logger.errors.INVALID_ARGUMENT, { + argument: "value", + value + })); + } return await Promise.all(value.map((v) => resolveAddresses(resolver, v, paramType.arrayChildren))); } diff --git a/packages/solidity/package.json b/packages/solidity/package.json index 66194493a..63a40ccc9 100644 --- a/packages/solidity/package.json +++ b/packages/solidity/package.json @@ -1,11 +1,12 @@ { "author": "Richard Moore ", "dependencies": { - "@ethersproject/bignumber": "^5.4.0", - "@ethersproject/bytes": "^5.4.0", - "@ethersproject/keccak256": "^5.4.0", - "@ethersproject/sha2": "^5.4.0", - "@ethersproject/strings": "^5.4.0" + "@ethersproject/bignumber": "^5.5.0", + "@ethersproject/bytes": "^5.5.0", + "@ethersproject/keccak256": "^5.5.0", + "@ethersproject/logger": "^5.5.0", + "@ethersproject/sha2": "^5.5.0", + "@ethersproject/strings": "^5.5.0" }, "description": "Solidity coder for non-standard (tight) packing.", "ethereum": "donations.ethers.eth", @@ -41,5 +42,5 @@ "sideEffects": false, "tarballHash": "0x44154ee5d7130c1d61b72b000f1c5acb7bfa7b0870dde55ea8dec7b6045a465d", "types": "./lib/index.d.ts", - "version": "5.4.0" + "version": "5.5.0" } diff --git a/packages/solidity/src.ts/index.ts b/packages/solidity/src.ts/index.ts index ff15fc839..2ad661e2c 100644 --- a/packages/solidity/src.ts/index.ts +++ b/packages/solidity/src.ts/index.ts @@ -12,6 +12,11 @@ const regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$"); const Zeros = "0000000000000000000000000000000000000000000000000000000000000000"; +import { Logger } from "@ethersproject/logger"; +import { version } from "./_version"; +const logger = new Logger(version); + + function _pack(type: string, value: any, isArray?: boolean): Uint8Array { switch(type) { case "address": @@ -33,7 +38,7 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array { let size = parseInt(match[2] || "256") if ((match[2] && String(size) !== match[2]) || (size % 8 !== 0) || size === 0 || size > 256) { - throw new Error("invalid number type - " + type); + logger.throwArgumentError("invalid number type", "type", type) } if (isArray) { size = 256; } @@ -48,9 +53,11 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array { const size = parseInt(match[1]); if (String(size) !== match[1] || size === 0 || size > 32) { - throw new Error("invalid bytes type - " + type); + logger.throwArgumentError("invalid bytes type", "type", type) + } + if (arrayify(value).byteLength !== size) { + logger.throwArgumentError(`invalid value for ${ type }`, "value", value) } - if (arrayify(value).byteLength !== size) { throw new Error("invalid value for " + type); } if (isArray) { return arrayify((value + Zeros).substring(0, 66)); } return value; } @@ -59,7 +66,9 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array { if (match && Array.isArray(value)) { const baseType = match[1]; const count = parseInt(match[2] || String(value.length)); - if (count != value.length) { throw new Error("invalid value for " + type); } + if (count != value.length) { + logger.throwArgumentError(`invalid array length for ${ type }`, "value", value) + } const result: Array = []; value.forEach(function(value) { result.push(_pack(baseType, value, true)); @@ -67,13 +76,15 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array { return concat(result); } - throw new Error("invalid type - " + type); + return logger.throwArgumentError("invalid type", "type", type) } // @TODO: Array Enum export function pack(types: ReadonlyArray, values: ReadonlyArray) { - if (types.length != values.length) { throw new Error("type/value count mismatch"); } + if (types.length != values.length) { + logger.throwArgumentError("wrong number of values; expected ${ types.length }", "values", values) + } const tight: Array = []; types.forEach(function(type, index) { tight.push(_pack(type, values[index]));