@@ -93,39 +93,51 @@ export async function downloadPacketFromObjectStore(
9393
9494 logger . debug ( "Downloading from object store" , { url : url . href } ) ;
9595
96- async function fetchWithRetry ( url :string , retries = 3 , delay = 500 ) :Promise < Response > {
97-
98- if ( ! r2 ) {
99- throw new Error ( "Object store credentials are not set" ) ;
100- }
101-
102- for ( let attempt = 1 ; attempt <= retries ; attempt ++ ) {
103- try {
104- const response = await r2 . fetch ( url ) ;
105- if ( response . ok ) return response ;
106-
107- // only retry on transient server/network errors
108- if ( response . status >= 500 && response . status < 600 ) {
109- throw new Error ( `Server Error : ${ response . statusText } ` ) ;
110- }
111-
112- // for other non server errors
113- throw new Error ( `non-retryable error :${ response . statusText } ` ) ;
114-
115- } catch ( error :any ) {
116- if ( attempt == retries ) throw error ;
117- logger . warn ( `Retrying object Download (attempt: ${ attempt } ) out of ${ retries } ` , {
118- url,
119- error :error . message ,
120- } ) ;
121- await new Promise ( ( res ) => setTimeout ( res , delay * attempt ) ) ;
96+
97+ async function fetchWithRetry ( url : string , retries = 3 , delay = 500 ) : Promise < Response > {
98+ for ( let attempt = 1 ; attempt <= retries ; attempt ++ ) {
99+ try {
100+ const response = await r2 . fetch ( url ) ;
101+ if ( response . ok ) return response ;
102+
103+ if ( response . status >= 400 && response . status < 500 ) {
104+ throw new Error ( `Client error (non-retryable): ${ response . statusText } ` ) ;
105+ }
106+
107+ if ( response . status >= 500 && response . status < 600 ) {
108+ if ( attempt === retries ) {
109+ throw new Error ( `Server error after ${ retries } attempts: ${ response . statusText } ` ) ;
122110 }
123- }
124111
125- throw new Error ( `Failed to fetch ${ url } after ${ retries } retries` ) ;
112+ logger . warn ( `Retrying object download (attempt ${ attempt } /${ retries } )` , {
113+ url,
114+ status : response . status ,
115+ error : response . statusText ,
116+ } ) ;
117+
118+ await new Promise ( ( res ) => setTimeout ( res , delay * attempt ) ) ;
119+ continue ;
120+ }
121+
122+ throw new Error ( `Unexpected status ${ response . status } : ${ response . statusText } ` ) ;
123+ } catch ( error : unknown ) {
124+ if ( attempt === retries ) throw error ;
125+
126+ const errorMessage = error instanceof Error ? error . message : String ( error ) ;
126127
128+ logger . warn ( `Network error, retrying (attempt ${ attempt } /${ retries } )` , {
129+ url,
130+ error : errorMessage ,
131+ } ) ;
132+
133+ await new Promise ( ( res ) => setTimeout ( res , delay * attempt ) ) ;
127134 }
128- const response = await fetchWithRetry ( url . toString ( ) ) ;
135+ }
136+
137+ throw new Error ( `Failed to fetch ${ url } after ${ retries } retries` ) ;
138+ }
139+
140+ const response = await fetchWithRetry ( url . toString ( ) ) ;
129141
130142 const data = await response . text ( ) ;
131143
0 commit comments