Skip to content

Commit

Permalink
fix(SequentialHandler): downlevel ECONNRESET errors (#8785)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckohen committed Oct 31, 2022
1 parent 0bcc18a commit 5a70057
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/rest/src/lib/handlers/SequentialHandler.ts
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/rest/src/lib/utils/utils.ts
Expand Up @@ -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');
}

0 comments on commit 5a70057

Please sign in to comment.