Skip to content

Commit 4753853

Browse files
committed
Retry for server-side gateway errors
1 parent 5ef9bf3 commit 4753853

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/client/Omaha.ts

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ export class Omaha extends EventEmitter<OmahaEvents> {
326326
}
327327
catch (error) {
328328
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+
329334
this.emit('error', error);
330335
this.emit('server_error', error);
331336
throw error;
@@ -335,36 +340,49 @@ export class Omaha extends EventEmitter<OmahaEvents> {
335340
throw new AbortError('The operation was aborted.');
336341
}
337342

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+
}
343350

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);
347363

348-
this._reattemptTasks.add(o);
349-
});
364+
if (this._reattemptFailed && (this._reattemptFailedCount === 0 || attempt < this._reattemptFailedCount)) {
365+
const o: any = {};
350366

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);
352370

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+
});
358373

359-
return this._fetchWithRetries(method, path, body, attempt + 1);
360-
}
374+
this._reattemptTasks.delete(o);
361375

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+
);
366380
}
381+
382+
return this._fetchWithRetries<T>(method, path, body, attempt + 1);
367383
}
384+
385+
throw error;
368386
}
369387

370388
/**

0 commit comments

Comments
 (0)