Added more information to some invalid argument errors (#1130).

This commit is contained in:
Richard Moore 2021-10-05 16:05:40 -04:00
parent bee76a49b2
commit f3c6d819f3
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
3 changed files with 30 additions and 13 deletions

View File

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

View File

@ -1,11 +1,12 @@
{
"author": "Richard Moore <me@ricmoo.com>",
"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"
}

View File

@ -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<Uint8Array> = [];
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<string>, values: ReadonlyArray<any>) {
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<Uint8Array> = [];
types.forEach(function(type, index) {
tight.push(_pack(type, values[index]));