@@ -65,24 +65,34 @@ export async function requestWithRetry(
65
65
maxRetries : number = MAX_RETRIES ,
66
66
) : Promise < http . IncomingMessage > {
67
67
let attempt = 0
68
+ let isRetryable = false
68
69
while ( attempt <= maxRetries ) {
69
70
try {
70
71
const response = await request ( transport , opt , body )
71
72
// Check if the HTTP status code is retryable
72
73
if ( isHttpRetryable ( response . statusCode as number ) ) {
74
+ isRetryable = true
73
75
throw new Error ( `Retryable HTTP status: ${ response . statusCode } ` ) // trigger retry attempt with calculated delay
74
76
}
77
+
75
78
return response // Success, return the raw response
76
79
} catch ( err ) {
77
- attempt ++
80
+ if ( isRetryable ) {
81
+ attempt ++
82
+ isRetryable = false
78
83
79
- if ( attempt > maxRetries ) {
80
- throw new Error ( `Request failed after ${ maxRetries } retries: ${ err } ` )
84
+ if ( attempt > maxRetries ) {
85
+ throw new Error ( `Request failed after ${ maxRetries } retries: ${ err } ` )
86
+ }
87
+ const delay = getExpBackOffDelay ( attempt )
88
+ // eslint-disable-next-line no-console
89
+ console . warn (
90
+ `${ new Date ( ) . toLocaleString ( ) } Retrying request (attempt ${ attempt } /${ maxRetries } ) after ${ delay } ms due to: ${ err } ` ,
91
+ )
92
+ await sleep ( delay )
93
+ } else {
94
+ throw err // re-throw if any request, syntax errors
81
95
}
82
- const delay = getExpBackOffDelay ( attempt )
83
- // eslint-disable-next-line no-console
84
- // console.warn( `${new Date().toLocaleString()} Retrying request (attempt ${attempt}/${maxRetries}) after ${delay}ms due to: ${err}`,)
85
- await sleep ( delay )
86
96
}
87
97
}
88
98
0 commit comments