-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(sdk): Make the "error handler" give more information (#777)
- Loading branch information
1 parent
58abb93
commit 6fd6a63
Showing
2 changed files
with
76 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,29 @@ | ||
import { BaseError, ContractFunctionRevertedError } from "viem"; | ||
import { ActionType } from "./constants"; | ||
|
||
export function extractErrorName(revertError: ContractFunctionRevertedError): string { | ||
if (revertError.data?.errorName) { | ||
return revertError.data.errorName; | ||
} | ||
if (revertError.signature) { | ||
return revertError.signature; | ||
} | ||
return "unknown revert reason"; | ||
} | ||
|
||
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 = extractErrorName(revertError); | ||
throw new Error(`${type} failed with ${errorName}`); | ||
} else { | ||
const errorMessage = err.shortMessage ?? "An unknown error occurred"; | ||
throw new Error(`${type} failed with ${errorMessage}`); | ||
} | ||
} 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"); | ||
} |