mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 13:03:31 +01:00
fix(SequentialHandler): downlevel ECONNRESET errors (#8785)
This commit is contained in:
@@ -8,7 +8,7 @@ import { DiscordAPIError, type DiscordErrorData, type OAuthErrorData } from '../
|
|||||||
import { HTTPError } from '../errors/HTTPError.js';
|
import { HTTPError } from '../errors/HTTPError.js';
|
||||||
import { RateLimitError } from '../errors/RateLimitError.js';
|
import { RateLimitError } from '../errors/RateLimitError.js';
|
||||||
import { RESTEvents } from '../utils/constants.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';
|
import type { IHandler } from './IHandler.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -307,8 +307,9 @@ export class SequentialHandler implements IHandler {
|
|||||||
try {
|
try {
|
||||||
res = await request(url, { ...options, signal: controller.signal });
|
res = await request(url, { ...options, signal: controller.signal });
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
// Retry the specified number of times for possible timed out requests
|
if (!(error instanceof Error)) throw error;
|
||||||
if (error instanceof Error && error.name === 'AbortError' && retries !== this.manager.options.retries) {
|
// Retry the specified number of times if needed
|
||||||
|
if (shouldRetry(error) && retries !== this.manager.options.retries) {
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
return await this.runRequest(routeId, url, options, requestData, ++retries);
|
return await this.runRequest(routeId, url, options, requestData, ++retries);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,3 +135,16 @@ export async function resolveBody(body: RequestInit['body']): Promise<RequestOpt
|
|||||||
|
|
||||||
throw new TypeError(`Unable to resolve body.`);
|
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');
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user