Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sdk): Make the "error handler" give more information #777

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions sdk/src/utils/errorHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe("errorHandler", () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe("handleError", () => {
const actionType: ActionType = ActionType.Transaction;

Expand All @@ -40,39 +41,60 @@ describe("errorHandler", () => {
expect(() => handleError(actionType, mockBaseError)).toThrow(`${actionType} failed with MockErrorName`);
});

it("should throw a generic error message if errorName is undefined", () => {
// Temporarily cast mockRevertedError.data to bypass TypeScript checks for testing
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(mockRevertedError.data as any).errorName = undefined; // Simulate errorName being `undefined`
it("should throw with the revert signature if errorName is undefined", () => {
(mockRevertedError.data as Partial<typeof mockRevertedError.data>).errorName = undefined;
mockRevertedError.signature = "myFunction(uint256)" as `0x${string}`;

jest.spyOn(mockBaseError, "walk").mockImplementation((fn: (arg0: unknown) => unknown) => {
return fn(mockRevertedError) ? mockRevertedError : null;
});

// This should test the code path where `errorName` is `undefined` and fallback to an empty string
expect(() => handleError(actionType, mockBaseError)).toThrow(`${actionType} failed with `);
expect(() => handleError(actionType, mockBaseError)).toThrow(`${actionType} failed with myFunction(uint256)`);
});

it("should throw a generic error message if it's an instance of BaseError but not ContractFunctionRevertedError", () => {
const mockBaseError = new BaseError("Base error");
it("should throw 'unknown revert reason' if both errorName and signature are undefined", () => {
(mockRevertedError.data as Partial<typeof mockRevertedError.data>).errorName = undefined;
mockRevertedError.signature = undefined;

jest.spyOn(mockBaseError, "walk").mockImplementation(() => null);
jest.spyOn(mockBaseError, "walk").mockImplementation((fn: (arg0: unknown) => unknown) => {
return fn(mockRevertedError) ? mockRevertedError : null;
});

expect(() => handleError(actionType, mockBaseError)).toThrow("${type} failed");
expect(() => handleError(actionType, mockBaseError)).toThrow(`${actionType} failed with unknown revert reason`);
});

it("should throw a generic error message if error is not an instance of BaseError", () => {
const genericError = "Something went wrong";
it("should throw with shortMessage if error is a BaseError but not ContractFunctionRevertedError", () => {
const shortMessage = "A short message";
const mockBaseErrorWithShortMessage = new BaseError("Base error");
mockBaseErrorWithShortMessage.shortMessage = shortMessage;

expect(() => handleError(actionType, genericError)).toThrow(`${actionType} failed with ${genericError}`);
jest.spyOn(mockBaseErrorWithShortMessage, "walk").mockImplementation(() => null);

expect(() => handleError(actionType, mockBaseErrorWithShortMessage)).toThrow(
`${actionType} failed with ${shortMessage}`,
);
});

it("should throw a generic error message even when there is no errorName in ContractFunctionRevertedError", () => {
jest.spyOn(mockBaseError, "walk").mockImplementation((fn: (arg0: unknown) => unknown) => {
return fn(mockRevertedError) ? mockRevertedError : null;
});
it("should throw 'An unknown error occurred' if no shortMessage is present", () => {
const mockBaseErrorWithoutShortMessage = new BaseError("Base error");

jest.spyOn(mockBaseErrorWithoutShortMessage, "walk").mockImplementation(() => null);

expect(() => handleError(actionType, mockBaseErrorWithoutShortMessage)).toThrow(
`${actionType} failed with An unknown error occurred`,
);
});

it("should throw with the error message if error is a native JavaScript Error", () => {
const nativeError = new Error("Native error message");

expect(() => handleError(actionType, nativeError)).toThrow(`${actionType} failed with Native error message`);
});

it("should throw 'unknown error' if the error is not an instance of BaseError or Error", () => {
const unknownError = { message: "Some unknown error" };

expect(() => handleError(actionType, mockBaseError)).toThrow(`${actionType} failed with `);
expect(() => handleError(actionType, unknownError)).toThrow(`${actionType} failed with an unknown error`);
});
});
});
11 changes: 7 additions & 4 deletions sdk/src/utils/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ export function handleError(type: ActionType, err: unknown): never {
if (err instanceof BaseError) {
const revertError = err.walk((err) => err instanceof ContractFunctionRevertedError);
if (revertError instanceof ContractFunctionRevertedError) {
const errorName = revertError.data?.errorName ?? "";
const errorName = revertError.data?.errorName ?? revertError.signature ?? "unknown revert reason";
Chirag-S-Kotian marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`${type} failed with ${errorName}`);
} else {
const shortMessage = err.shortMessage ?? "An unknown error occurred";
Chirag-S-Kotian marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`${type} failed with ${shortMessage}`);
}
} else if (err instanceof Error) {
throw new Error(`${type} failed with ${err.message}`);
} else {
throw new Error(`${type} failed with ${err}`);
throw new Error(`${type} failed with an unknown error`);
}

throw new Error("${type} failed");
}
Loading