@@ -56,10 +56,12 @@ const retryWithBackoff: BaseQueryEnhancer<
56
56
RetryOptions ,
57
57
RetryOptions | void
58
58
> = ( baseQuery , defaultOptions ) => async ( args , api , extraOptions ) => {
59
+ const defaultShouldRetry : Exclude < RetryOptions [ 'shouldRetry' ] , undefined > = ( _ , __ , { attempt, maxRetries} ) => attempt <= maxRetries
60
+
59
61
const options = {
60
62
maxRetries : 5 ,
61
63
backoff : defaultBackoff ,
62
- shouldRetry : ( ) => true ,
64
+ shouldRetry : defaultShouldRetry ,
63
65
...defaultOptions ,
64
66
...extraOptions ,
65
67
}
@@ -69,25 +71,30 @@ const retryWithBackoff: BaseQueryEnhancer<
69
71
try {
70
72
const result = await baseQuery ( args , api , extraOptions )
71
73
// 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 ) {
78
75
throw new HandledError ( result )
79
76
}
80
77
return result
81
78
} catch ( e : any ) {
82
79
retry ++
83
- if ( e . throwImmediately || retry > options . maxRetries ) {
80
+
81
+ if ( e . throwImmediately ) {
84
82
if ( e instanceof HandledError ) {
85
83
return e . value
86
84
}
87
85
88
86
// We don't know what this is, so we have to rethrow it
89
87
throw e
90
88
}
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
+ }
91
98
await options . backoff ( retry , options . maxRetries )
92
99
}
93
100
}
0 commit comments