-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ RPC_URL= | |
BUNDLER_URL= | ||
BICONOMY_SDK_DEBUG=false | ||
PAYMASTER_URL= | ||
PIMLICO_API_KEY= | ||
PIMLICO_API_KEY= | ||
TENDERLY_API_KEY= |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { BaseError } from "viem" | ||
export type KnownError = { | ||
name: string | ||
regex: string | ||
description: string | ||
causes: string[] | ||
solutions: string[] | ||
docsUrl?: string | ||
} | ||
|
||
export const ERRORS_URL = | ||
"https://raw.githubusercontent.com/bcnmy/aa-errors/main/docs/errors.json" | ||
export const DOCS_URL = "https://docs.biconomy.io/troubleshooting/commonerrors" | ||
const UNKOWN_ERROR_CODE = "520" | ||
|
||
const knownErrors: KnownError[] = [] | ||
|
||
const matchError = (message: string): null | KnownError => | ||
knownErrors.find( | ||
(knownError: KnownError) => | ||
message.toLowerCase().indexOf(knownError.regex.toLowerCase()) > -1 | ||
) ?? null | ||
|
||
const buildErrorStrings = (error: KnownError, status: string): string[] => | ||
[ | ||
`${status}: ${error.description}\n`, | ||
error.causes?.length | ||
? ["Potential cause(s): \n", ...error.causes, ""].join("\n") | ||
: "", | ||
error.solutions?.length | ||
? ["Potential solution(s): \n", ...error.solutions].join("\n") | ||
: "" | ||
].filter(Boolean) | ||
|
||
type AccountAbstractionErrorParams = { | ||
docsSlug?: string | ||
metaMessages?: string[] | ||
details?: string | ||
} | ||
|
||
class AccountAbstractionError extends BaseError { | ||
override name = "AccountAbstractionError" | ||
override version = "@biconomy/sdk" | ||
|
||
constructor(title: string, params: AccountAbstractionErrorParams = {}) { | ||
super(title, params) | ||
} | ||
} | ||
|
||
export const getAAError = async (message: string, httpStatus?: number) => { | ||
if (!knownErrors.length) { | ||
const errors = (await (await fetch(ERRORS_URL)).json()) as KnownError[] | ||
knownErrors.push(...errors) | ||
} | ||
|
||
const details: string = JSON.stringify(message) | ||
const matchedError = matchError(details) | ||
const status = | ||
matchedError?.regex ?? (httpStatus ?? UNKOWN_ERROR_CODE).toString() | ||
|
||
const metaMessages = matchedError | ||
? buildErrorStrings(matchedError, status) | ||
: [] | ||
const title = matchedError ? matchedError.name : "Unknown Error" | ||
const docsSlug = matchedError?.docsUrl ?? DOCS_URL | ||
|
||
return new AccountAbstractionError(title, { | ||
docsSlug, | ||
metaMessages, | ||
details | ||
}) | ||
} |
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