Skip to content

Commit

Permalink
Extract error code from web3 API response
Browse files Browse the repository at this point in the history
  • Loading branch information
h0ngcha0 committed Aug 15, 2024
1 parent cf0c46d commit ce77bab
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/extension/src/shared/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ export const getErrorObject = (error: any, includeStack = true) => {
// ignore parsing error
}
}

const statusCodeRegex = /Status\s+code:\s+(\d{3})/
export const extractStatusCode = (error: any): number | undefined => {
const match = (error instanceof Error) && error.message.match(statusCodeRegex)
if (match) {
return Number(match[1])
}
}
3 changes: 2 additions & 1 deletion packages/extension/src/ui/services/swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import useSWR, {
} from "swr"

import { reviveJsonBigNumber } from "../../shared/json"
import { extractStatusCode } from "../../shared/utils/error"

export interface SWRConfigCommon {
suspense?: boolean
Expand Down Expand Up @@ -106,6 +107,6 @@ export const swrCacheProvider: Cache = {
}

export const retryWhenRateLimited = (error: any) => {
const errorCode = error?.status || error?.errorCode
const errorCode = error?.status || error?.errorCode || extractStatusCode(error)
return errorCode && errorCode === 429
}

0 comments on commit ce77bab

Please sign in to comment.