From 9932dcd97f7282d38f1e597969b9bc8453a02075 Mon Sep 17 00:00:00 2001 From: kahirokunn Date: Thu, 14 Apr 2022 14:48:40 +0900 Subject: [PATCH] retry condition by http error response --- packages/toolkit/src/query/retry.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/toolkit/src/query/retry.ts b/packages/toolkit/src/query/retry.ts index 9fa9c6f690..fede52ecba 100644 --- a/packages/toolkit/src/query/retry.ts +++ b/packages/toolkit/src/query/retry.ts @@ -1,4 +1,5 @@ import type { BaseQueryEnhancer } from './baseQueryTypes' +import { FetchBaseQueryError } from './fetchBaseQuery' import { HandledError } from './HandledError' /** @@ -32,6 +33,10 @@ export interface RetryOptions { * Function used to determine delay between retries */ backoff?: (attempt: number, maxRetries: number) => Promise + /** + * Function used to determine need retry error + */ + needRetryError?: (error: FetchBaseQueryError) => boolean } function fail(e: any): never { @@ -48,6 +53,7 @@ const retryWithBackoff: BaseQueryEnhancer< const options = { maxRetries: 5, backoff: defaultBackoff, + needRetryError: () => true, ...defaultOptions, ...extraOptions, } @@ -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