diff --git a/src/lib/async/retry.ts b/src/lib/async/retry.ts index f14ae6cf66..2b605c442f 100644 --- a/src/lib/async/retry.ts +++ b/src/lib/async/retry.ts @@ -1,9 +1,30 @@ +import {XRPCError} from '@atproto/xrpc' + import {isNetworkError} from 'lib/strings/errors' +export const NETWORK_FAILURE_STATUSES = [ + 1, 408, 425, 429, 500, 502, 503, 504, 522, 524, +] + +function isRetryable(e: unknown) { + if (e instanceof XRPCError) { + if (NETWORK_FAILURE_STATUSES.includes(e.status)) { + return true + } + } + + return isNetworkError(e) +} + export async function retry
( - retries: number, - cond: (err: any) => boolean, fn: () => Promise
, + { + retries, + checkIsRetryable = isRetryable, + }: { + retries: number + checkIsRetryable?: (err: any) => boolean + }, ): Promise
{ let lastErr while (retries > 0) { @@ -11,7 +32,7 @@ export async function retry
( return await fn() } catch (e: any) { lastErr = e - if (cond(e)) { + if (checkIsRetryable(e)) { retries-- continue } @@ -25,5 +46,7 @@ export async function networkRetry
( retries: number, fn: () => Promise
, ): Promise
{ - return retry(retries, isNetworkError, fn) + return retry(fn, { + retries, + }) }