@@ -65,24 +65,34 @@ export async function requestWithRetry(
6565 maxRetries : number = MAX_RETRIES ,
6666) : Promise < http . IncomingMessage > {
6767 let attempt = 0
68+ let isRetryable = false
6869 while ( attempt <= maxRetries ) {
6970 try {
7071 const response = await request ( transport , opt , body )
7172 // Check if the HTTP status code is retryable
7273 if ( isHttpRetryable ( response . statusCode as number ) ) {
74+ isRetryable = true
7375 throw new Error ( `Retryable HTTP status: ${ response . statusCode } ` ) // trigger retry attempt with calculated delay
7476 }
77+
7578 return response // Success, return the raw response
7679 } catch ( err ) {
77- attempt ++
80+ if ( isRetryable ) {
81+ attempt ++
82+ isRetryable = false
7883
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
8195 }
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 )
8696 }
8797 }
8898
0 commit comments