Skip to content

Commit

Permalink
api: handle json in engine error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian committed Oct 10, 2024
1 parent 54bcec4 commit b5ea4bf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions apps/api/src/utils/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,12 @@ describe("error utils", () => {
expect(normalizeEngineErrorMessage("Unknown Error - test error")).toBe(
"Unknown Error - test error",
);
expect(
normalizeEngineErrorMessage(
'{"code":3,"message":"failed with 50004597 gas: insufficient funds for gas * price + value: address 0xafCB379C0B76265bCb8432ab776A2641F0355Eb3 have 0 want 100000000000000"}',
),
).toBe(
"failed with 50004597 gas: insufficient funds for gas * price + value: address 0xafCB379C0B76265bCb8432ab776A2641F0355Eb3 have 0 want 100000000000000",
);
});
});
11 changes: 10 additions & 1 deletion apps/api/src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,17 @@ const ENGINE_ERROR_MESSAGE_MAP = {
} as const;

export const normalizeEngineErrorMessage = (rawMessage: string) => {
try {
const error: object = JSON.parse(rawMessage);
if ("message" in error && typeof error.message === "string") {
return error.message;
}
} catch {
// Ignore error if message wasn't valid JSON and try regular expression matches instead
}

const groups =
/(?:reason: '(.*?)' at)|(?:reason="execution reverted: (.*?)")|(?:eth_sendUserOperation error: {"message":"(.*?)"|(?:Simulation failed: TransactionError: Error - (.*))|(?:^Simulation failed: (.*))|(?:^Error - (.*)))/gi.exec(
/(?:reason: '(.*?)' at)|(?:reason="execution reverted: (.*?)")|(?:eth_sendUserOperation error: {"message":"(.*?)")|(?:Simulation failed: TransactionError: Error - (.*))|(?:^Simulation failed: (.*))|(?:^Error - (.*))/gi.exec(
rawMessage,
);
const message = groups?.slice(1).find((group) => group) ?? rawMessage;
Expand Down

0 comments on commit b5ea4bf

Please sign in to comment.