Skip to content

Commit f3c6d81

Browse files
committed
Added more information to some invalid argument errors (ethers-io#1130).
1 parent bee76a4 commit f3c6d81

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

packages/contracts/src.ts/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ async function resolveAddresses(resolver: Signer | Provider, value: any, paramTy
157157
}
158158

159159
if (paramType.baseType === "array") {
160-
if (!Array.isArray(value)) { return Promise.reject(new Error("invalid value for array")); }
160+
if (!Array.isArray(value)) {
161+
return Promise.reject(logger.makeError("invalid value for array", Logger.errors.INVALID_ARGUMENT, {
162+
argument: "value",
163+
value
164+
}));
165+
}
161166
return await Promise.all(value.map((v) => resolveAddresses(resolver, v, paramType.arrayChildren)));
162167
}
163168

packages/solidity/package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"author": "Richard Moore <[email protected]>",
33
"dependencies": {
4-
"@ethersproject/bignumber": "^5.4.0",
5-
"@ethersproject/bytes": "^5.4.0",
6-
"@ethersproject/keccak256": "^5.4.0",
7-
"@ethersproject/sha2": "^5.4.0",
8-
"@ethersproject/strings": "^5.4.0"
4+
"@ethersproject/bignumber": "^5.5.0",
5+
"@ethersproject/bytes": "^5.5.0",
6+
"@ethersproject/keccak256": "^5.5.0",
7+
"@ethersproject/logger": "^5.5.0",
8+
"@ethersproject/sha2": "^5.5.0",
9+
"@ethersproject/strings": "^5.5.0"
910
},
1011
"description": "Solidity coder for non-standard (tight) packing.",
1112
"ethereum": "donations.ethers.eth",
@@ -41,5 +42,5 @@
4142
"sideEffects": false,
4243
"tarballHash": "0x44154ee5d7130c1d61b72b000f1c5acb7bfa7b0870dde55ea8dec7b6045a465d",
4344
"types": "./lib/index.d.ts",
44-
"version": "5.4.0"
45+
"version": "5.5.0"
4546
}

packages/solidity/src.ts/index.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ const regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$");
1212

1313
const Zeros = "0000000000000000000000000000000000000000000000000000000000000000";
1414

15+
import { Logger } from "@ethersproject/logger";
16+
import { version } from "./_version";
17+
const logger = new Logger(version);
18+
19+
1520
function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
1621
switch(type) {
1722
case "address":
@@ -33,7 +38,7 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
3338
let size = parseInt(match[2] || "256")
3439

3540
if ((match[2] && String(size) !== match[2]) || (size % 8 !== 0) || size === 0 || size > 256) {
36-
throw new Error("invalid number type - " + type);
41+
logger.throwArgumentError("invalid number type", "type", type)
3742
}
3843

3944
if (isArray) { size = 256; }
@@ -48,9 +53,11 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
4853
const size = parseInt(match[1]);
4954

5055
if (String(size) !== match[1] || size === 0 || size > 32) {
51-
throw new Error("invalid bytes type - " + type);
56+
logger.throwArgumentError("invalid bytes type", "type", type)
57+
}
58+
if (arrayify(value).byteLength !== size) {
59+
logger.throwArgumentError(`invalid value for ${ type }`, "value", value)
5260
}
53-
if (arrayify(value).byteLength !== size) { throw new Error("invalid value for " + type); }
5461
if (isArray) { return arrayify((value + Zeros).substring(0, 66)); }
5562
return value;
5663
}
@@ -59,21 +66,25 @@ function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
5966
if (match && Array.isArray(value)) {
6067
const baseType = match[1];
6168
const count = parseInt(match[2] || String(value.length));
62-
if (count != value.length) { throw new Error("invalid value for " + type); }
69+
if (count != value.length) {
70+
logger.throwArgumentError(`invalid array length for ${ type }`, "value", value)
71+
}
6372
const result: Array<Uint8Array> = [];
6473
value.forEach(function(value) {
6574
result.push(_pack(baseType, value, true));
6675
});
6776
return concat(result);
6877
}
6978

70-
throw new Error("invalid type - " + type);
79+
return logger.throwArgumentError("invalid type", "type", type)
7180
}
7281

7382
// @TODO: Array Enum
7483

7584
export function pack(types: ReadonlyArray<string>, values: ReadonlyArray<any>) {
76-
if (types.length != values.length) { throw new Error("type/value count mismatch"); }
85+
if (types.length != values.length) {
86+
logger.throwArgumentError("wrong number of values; expected ${ types.length }", "values", values)
87+
}
7788
const tight: Array<Uint8Array> = [];
7889
types.forEach(function(type, index) {
7990
tight.push(_pack(type, values[index]));

0 commit comments

Comments
 (0)