Skip to content

Commit e36b6c3

Browse files
committed
Better error messages when parsing fragment strings (#4246).
1 parent 450a176 commit e36b6c3

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src.ts/abi/fragments.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,11 @@ export class ParamType {
795795
if (ParamType.isParamType(obj)) { return obj; }
796796

797797
if (typeof(obj) === "string") {
798-
return ParamType.from(lex(obj), allowIndexed);
798+
try {
799+
return ParamType.from(lex(obj), allowIndexed);
800+
} catch (error) {
801+
assertArgument(false, "invalid param type", "obj", obj);
802+
}
799803

800804
} else if (obj instanceof TokenString) {
801805
let type = "", baseType = "";
@@ -1159,7 +1163,11 @@ export class EventFragment extends NamedFragment {
11591163
if (EventFragment.isFragment(obj)) { return obj; }
11601164

11611165
if (typeof(obj) === "string") {
1162-
return EventFragment.from(lex(obj));
1166+
try {
1167+
return EventFragment.from(lex(obj));
1168+
} catch (error) {
1169+
assertArgument(false, "invalid event fragment", "obj", obj);
1170+
}
11631171

11641172
} else if (obj instanceof TokenString) {
11651173
const name = consumeName("event", obj);
@@ -1237,7 +1245,11 @@ export class ConstructorFragment extends Fragment {
12371245
if (ConstructorFragment.isFragment(obj)) { return obj; }
12381246

12391247
if (typeof(obj) === "string") {
1240-
return ConstructorFragment.from(lex(obj));
1248+
try {
1249+
return ConstructorFragment.from(lex(obj));
1250+
} catch (error) {
1251+
assertArgument(false, "invalid constuctor fragment", "obj", obj);
1252+
}
12411253

12421254
} else if (obj instanceof TokenString) {
12431255
consumeKeywords(obj, setify([ "constructor" ]));
@@ -1300,7 +1312,11 @@ export class FallbackFragment extends Fragment {
13001312
if (FallbackFragment.isFragment(obj)) { return obj; }
13011313

13021314
if (typeof(obj) === "string") {
1303-
return FallbackFragment.from(lex(obj));
1315+
try {
1316+
return FallbackFragment.from(lex(obj));
1317+
} catch (error) {
1318+
assertArgument(false, "invalid fallback fragment", "obj", obj);
1319+
}
13041320

13051321
} else if (obj instanceof TokenString) {
13061322
const errorObj = obj.toString();
@@ -1472,7 +1488,11 @@ export class FunctionFragment extends NamedFragment {
14721488
if (FunctionFragment.isFragment(obj)) { return obj; }
14731489

14741490
if (typeof(obj) === "string") {
1475-
return FunctionFragment.from(lex(obj));
1491+
try {
1492+
return FunctionFragment.from(lex(obj));
1493+
} catch (error) {
1494+
assertArgument(false, "invalid function fragment", "obj", obj);
1495+
}
14761496

14771497
} else if (obj instanceof TokenString) {
14781498
const name = consumeName("function", obj);
@@ -1553,7 +1573,11 @@ export class StructFragment extends NamedFragment {
15531573
*/
15541574
static from(obj: any): StructFragment {
15551575
if (typeof(obj) === "string") {
1556-
return StructFragment.from(lex(obj));
1576+
try {
1577+
return StructFragment.from(lex(obj));
1578+
} catch (error) {
1579+
assertArgument(false, "invalid struct fragment", "obj", obj);
1580+
}
15571581

15581582
} else if (obj instanceof TokenString) {
15591583
const name = consumeName("struct", obj);

0 commit comments

Comments
 (0)