Skip to content

Commit 0f62792

Browse files
kahirokunnmarkerikson
authored andcommitted
chore: retry > options.maxRetries has been eliminated and only shouldRetry is used to determine if a retry is needed.
1 parent 89cab7a commit 0f62792

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

packages/toolkit/src/query/retry.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ const retryWithBackoff: BaseQueryEnhancer<
5656
RetryOptions,
5757
RetryOptions | void
5858
> = (baseQuery, defaultOptions) => async (args, api, extraOptions) => {
59+
const defaultShouldRetry: Exclude<RetryOptions['shouldRetry'], undefined> = (_, __, {attempt, maxRetries}) => attempt <= maxRetries
60+
5961
const options = {
6062
maxRetries: 5,
6163
backoff: defaultBackoff,
62-
shouldRetry: () => true,
64+
shouldRetry: defaultShouldRetry,
6365
...defaultOptions,
6466
...extraOptions,
6567
}
@@ -69,25 +71,30 @@ const retryWithBackoff: BaseQueryEnhancer<
6971
try {
7072
const result = await baseQuery(args, api, extraOptions)
7173
// baseQueries _should_ return an error property, so we should check for that and throw it to continue retrying
72-
if (result.error && options.shouldRetry(result.error as FetchBaseQueryError, args, {
73-
attempt: retry,
74-
maxRetries: options.maxRetries,
75-
baseQueryApi: api,
76-
extraOptions
77-
})) {
74+
if (result.error) {
7875
throw new HandledError(result)
7976
}
8077
return result
8178
} catch (e: any) {
8279
retry++
83-
if (e.throwImmediately || retry > options.maxRetries) {
80+
81+
if (e.throwImmediately) {
8482
if (e instanceof HandledError) {
8583
return e.value
8684
}
8785

8886
// We don't know what this is, so we have to rethrow it
8987
throw e
9088
}
89+
90+
if (e instanceof HandledError && !options.shouldRetry(e.value as FetchBaseQueryError, args, {
91+
attempt: retry,
92+
maxRetries: options.maxRetries,
93+
baseQueryApi: api,
94+
extraOptions
95+
})) {
96+
return e.value
97+
}
9198
await options.backoff(retry, options.maxRetries)
9299
}
93100
}

0 commit comments

Comments
 (0)