@@ -326,6 +326,11 @@ export class Omaha extends EventEmitter<OmahaEvents> {
326
326
}
327
327
catch ( error ) {
328
328
if ( error instanceof HttpError ) {
329
+ // Treat 502-504 as connection errors so we retry them
330
+ if ( error . code >= 502 && error . code <= 504 ) {
331
+ return this . _handleClientError < T > ( method , path , body , error , attempt ) ;
332
+ }
333
+
329
334
this . emit ( 'error' , error ) ;
330
335
this . emit ( 'server_error' , error ) ;
331
336
throw error ;
@@ -335,36 +340,49 @@ export class Omaha extends EventEmitter<OmahaEvents> {
335
340
throw new AbortError ( 'The operation was aborted.' ) ;
336
341
}
337
342
338
- this . emit ( 'error' , error ) ;
339
- this . emit ( 'client_error' , error , attempt , this . _reattemptFailed ? this . _reattemptFailedCount : undefined ) ;
340
-
341
- if ( this . _reattemptFailed && ( this . _reattemptFailedCount === 0 || attempt < this . _reattemptFailedCount ) ) {
342
- const o : any = { } ;
343
+ return this . _handleClientError < T > ( method , path , body , error , attempt ) ;
344
+ }
345
+ else {
346
+ throw new Error ( `Caught non-error of type ${ typeof error } within fetch - this should never happen!` ) ;
347
+ }
348
+ }
349
+ }
343
350
344
- const resume = await new Promise < boolean > ( resolve => {
345
- o . timeout = setTimeout ( ( ) => resolve ( true ) , this . _reattemptFailedDelay ) ;
346
- o . cancel = ( ) => resolve ( false ) ;
351
+ /**
352
+ * Handles the given client error and retries the request as cnofigured.
353
+ * @param method
354
+ * @param path
355
+ * @param body
356
+ * @param error
357
+ * @param attempt
358
+ * @returns
359
+ */
360
+ private async _handleClientError < T > ( method : string , path : string , body : PostOptions | undefined , error : Error , attempt : number ) {
361
+ this . emit ( 'error' , error ) ;
362
+ this . emit ( 'client_error' , error , attempt , this . _reattemptFailed ? this . _reattemptFailedCount : undefined ) ;
347
363
348
- this . _reattemptTasks . add ( o ) ;
349
- } ) ;
364
+ if ( this . _reattemptFailed && ( this . _reattemptFailedCount === 0 || attempt < this . _reattemptFailedCount ) ) {
365
+ const o : any = { } ;
350
366
351
- this . _reattemptTasks . delete ( o ) ;
367
+ const resume = await new Promise < boolean > ( resolve => {
368
+ o . timeout = setTimeout ( ( ) => resolve ( true ) , this . _reattemptFailedDelay ) ;
369
+ o . cancel = ( ) => resolve ( false ) ;
352
370
353
- if ( ! resume ) {
354
- throw new AbortError (
355
- 'The request was aborted while attempting to recover from an error:\n' + error . stack
356
- ) ;
357
- }
371
+ this . _reattemptTasks . add ( o ) ;
372
+ } ) ;
358
373
359
- return this . _fetchWithRetries ( method , path , body , attempt + 1 ) ;
360
- }
374
+ this . _reattemptTasks . delete ( o ) ;
361
375
362
- throw error ;
363
- }
364
- else {
365
- throw new Error ( `Caught non-error of type ${ typeof error } within fetch - this should never happen!` ) ;
376
+ if ( ! resume ) {
377
+ throw new AbortError (
378
+ 'The request was aborted while attempting to recover from an error:\n' + error . stack
379
+ ) ;
366
380
}
381
+
382
+ return this . _fetchWithRetries < T > ( method , path , body , attempt + 1 ) ;
367
383
}
384
+
385
+ throw error ;
368
386
}
369
387
370
388
/**
0 commit comments