fix(SequentialHandler): downlevel ECONNRESET errors (#8785)

This commit is contained in:
ckohen
2022-10-31 11:03:38 -07:00
committed by GitHub
parent 0bcc18a0bd
commit 5a70057826
2 changed files with 17 additions and 3 deletions

View File

@@ -8,7 +8,7 @@ import { DiscordAPIError, type DiscordErrorData, type OAuthErrorData } from '../
import { HTTPError } from '../errors/HTTPError.js';
import { RateLimitError } from '../errors/RateLimitError.js';
import { RESTEvents } from '../utils/constants.js';
import { hasSublimit, parseHeader, parseResponse } from '../utils/utils.js';
import { hasSublimit, parseHeader, parseResponse, shouldRetry } from '../utils/utils.js';
import type { IHandler } from './IHandler.js';
/**
@@ -307,8 +307,9 @@ export class SequentialHandler implements IHandler {
try {
res = await request(url, { ...options, signal: controller.signal });
} catch (error: unknown) {
// Retry the specified number of times for possible timed out requests
if (error instanceof Error && error.name === 'AbortError' && retries !== this.manager.options.retries) {
if (!(error instanceof Error)) throw error;
// Retry the specified number of times if needed
if (shouldRetry(error) && retries !== this.manager.options.retries) {
// eslint-disable-next-line no-param-reassign
return await this.runRequest(routeId, url, options, requestData, ++retries);
}

View File

@@ -135,3 +135,16 @@ export async function resolveBody(body: RequestInit['body']): Promise<RequestOpt
throw new TypeError(`Unable to resolve body.`);
}
/**
* Check whether an error indicates that a retry can be attempted
*
* @param error - The error thrown by the network request
* @returns Whether the error indicates a retry should be attempted
*/
export function shouldRetry(error: Error | NodeJS.ErrnoException) {
// Retry for possible timed out requests
if (error.name === 'AbortError') return true;
// Downlevel ECONNRESET to retry as it may be recoverable
return ('code' in error && error.code === 'ECONNRESET') || error.message.includes('ECONNRESET');
}