Skip to content

Commit

Permalink
More robust JSON-RPC error handling for reverted executions (#2603).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Mar 25, 2022
1 parent bc400c7 commit 9d9b14b
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions packages/providers/src.ts/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,48 @@ import { BaseProvider, Event } from "./base-provider";

const errorGas = [ "call", "estimateGas" ];

function spelunk(value: any): null | { message: string, data: string } {
if (value == null) { return null; }

// These *are* the droids we're looking for.
if (typeof(value.message) === "string" && value.message.match("reverted") && isHexString(value.data)) {
return { message: value.message, data: value.data };
}

// Spelunk further...
if (typeof(value) === "object") {
for (const key in value) {
const result = spelunk(value[key]);
if (result) { return result; }
}
return null;
}

// Might be a JSON string we can further descend...
if (typeof(value) === "string") {
try {
return spelunk(JSON.parse(value));
} catch (error) { }
}

return null;
}

function checkError(method: string, error: any, params: any): any {

// Undo the "convenience" some nodes are attempting to prevent backwards
// incompatibility; maybe for v6 consider forwarding reverts as errors
if (method === "call" && error.code === Logger.errors.SERVER_ERROR) {
const e = error.error;
if (e && e.message.match("reverted") && isHexString(e.data)) {
return e.data;
}
if (method === "call") {
const result = spelunk(error);
if (result) { return result.data; }

logger.throwError("missing revert data in call exception", Logger.errors.CALL_EXCEPTION, {
error, data: "0x"
});
}

// @TODO: Should we spelunk for message too?

let message = error.message;
if (error.code === Logger.errors.SERVER_ERROR && error.error && typeof(error.error.message) === "string") {
message = error.error.message;
Expand Down

0 comments on commit 9d9b14b

Please sign in to comment.