Compare commits

...

2 Commits

Author SHA1 Message Date
Eric Bailey 0c50a36053 Better naming 2024-05-22 13:39:52 -05:00
Eric Bailey c7052187ea Check for retryable status codes also 2024-05-22 13:05:38 -05:00
+27 -4
View File
@@ -1,9 +1,30 @@
import {XRPCError} from '@atproto/xrpc'
import {isNetworkError} from 'lib/strings/errors'
export const RETRYABLE_NETWORK_STATUS_CODES = [
1, 408, 425, 429, 500, 502, 503, 504, 522, 524,
]
function isRetryableError(e: unknown) {
if (e instanceof XRPCError) {
if (RETRYABLE_NETWORK_STATUS_CODES.includes(e.status)) {
return true
}
}
return isNetworkError(e)
}
export async function retry<P>(
retries: number,
cond: (err: any) => boolean,
fn: () => Promise<P>,
{
retries,
checkIsRetryableError = isRetryableError,
}: {
retries: number
checkIsRetryableError?: (err: any) => boolean
},
): Promise<P> {
let lastErr
while (retries > 0) {
@@ -11,7 +32,7 @@ export async function retry<P>(
return await fn()
} catch (e: any) {
lastErr = e
if (cond(e)) {
if (checkIsRetryableError(e)) {
retries--
continue
}
@@ -25,5 +46,7 @@ export async function networkRetry<P>(
retries: number,
fn: () => Promise<P>,
): Promise<P> {
return retry(retries, isNetworkError, fn)
return retry(fn, {
retries,
})
}