Skip to content

Commit

Permalink
Better error messages when parsing fragment strings (#4246).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jul 23, 2023
1 parent 450a176 commit e36b6c3
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src.ts/abi/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,11 @@ export class ParamType {
if (ParamType.isParamType(obj)) { return obj; }

if (typeof(obj) === "string") {
return ParamType.from(lex(obj), allowIndexed);
try {
return ParamType.from(lex(obj), allowIndexed);
} catch (error) {
assertArgument(false, "invalid param type", "obj", obj);
}

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

if (typeof(obj) === "string") {
return EventFragment.from(lex(obj));
try {
return EventFragment.from(lex(obj));
} catch (error) {
assertArgument(false, "invalid event fragment", "obj", obj);
}

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

if (typeof(obj) === "string") {
return ConstructorFragment.from(lex(obj));
try {
return ConstructorFragment.from(lex(obj));
} catch (error) {
assertArgument(false, "invalid constuctor fragment", "obj", obj);
}

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

if (typeof(obj) === "string") {
return FallbackFragment.from(lex(obj));
try {
return FallbackFragment.from(lex(obj));
} catch (error) {
assertArgument(false, "invalid fallback fragment", "obj", obj);
}

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

if (typeof(obj) === "string") {
return FunctionFragment.from(lex(obj));
try {
return FunctionFragment.from(lex(obj));
} catch (error) {
assertArgument(false, "invalid function fragment", "obj", obj);
}

} else if (obj instanceof TokenString) {
const name = consumeName("function", obj);
Expand Down Expand Up @@ -1553,7 +1573,11 @@ export class StructFragment extends NamedFragment {
*/
static from(obj: any): StructFragment {
if (typeof(obj) === "string") {
return StructFragment.from(lex(obj));
try {
return StructFragment.from(lex(obj));
} catch (error) {
assertArgument(false, "invalid struct fragment", "obj", obj);
}

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

0 comments on commit e36b6c3

Please sign in to comment.