Skip to content

Commit

Permalink
retry condition by http error response
Browse files Browse the repository at this point in the history
  • Loading branch information
kahirokunn committed Apr 14, 2022
1 parent f098c92 commit 9932dcd
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/toolkit/src/query/retry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BaseQueryEnhancer } from './baseQueryTypes'
import { FetchBaseQueryError } from './fetchBaseQuery'
import { HandledError } from './HandledError'

/**
Expand Down Expand Up @@ -32,6 +33,10 @@ export interface RetryOptions {
* Function used to determine delay between retries
*/
backoff?: (attempt: number, maxRetries: number) => Promise<void>
/**
* Function used to determine need retry error
*/
needRetryError?: (error: FetchBaseQueryError) => boolean
}

function fail(e: any): never {
Expand All @@ -48,6 +53,7 @@ const retryWithBackoff: BaseQueryEnhancer<
const options = {
maxRetries: 5,
backoff: defaultBackoff,
needRetryError: () => true,
...defaultOptions,
...extraOptions,
}
Expand All @@ -57,7 +63,7 @@ const retryWithBackoff: BaseQueryEnhancer<
try {
const result = await baseQuery(args, api, extraOptions)
// baseQueries _should_ return an error property, so we should check for that and throw it to continue retrying
if (result.error) {
if (result.error && options.needRetryError(result.error as FetchBaseQueryError)) {
throw new HandledError(result)
}
return result
Expand Down

0 comments on commit 9932dcd

Please sign in to comment.