diff --git a/packages/ws/src/utils/constants.ts b/packages/ws/src/utils/constants.ts index 2917090eaaff..f14a114955a4 100644 --- a/packages/ws/src/utils/constants.ts +++ b/packages/ws/src/utils/constants.ts @@ -82,3 +82,5 @@ export function getInitialSendRateLimitState(): SendRateLimitState { resetAt: Date.now() + 60_000, }; } + +export const KnownNetworkErrorCodes = new Set(['ECONNRESET', 'ECONNREFUSED', 'ETIMEDOUT']); diff --git a/packages/ws/src/ws/WebSocketShard.ts b/packages/ws/src/ws/WebSocketShard.ts index 1e3fcaed5a7e..f108210cce62 100644 --- a/packages/ws/src/ws/WebSocketShard.ts +++ b/packages/ws/src/ws/WebSocketShard.ts @@ -26,6 +26,7 @@ import { CompressionMethod, CompressionParameterMap, ImportantGatewayOpcodes, + KnownNetworkErrorCodes, getInitialSendRateLimitState, } from '../utils/constants.js'; import type { SessionInfo } from './WebSocketManager.js'; @@ -113,7 +114,7 @@ export class WebSocketShard extends AsyncEventEmitter { // Indicates whether the shard has already resolved its original connect() call private initialConnectResolved = false; - // Indicates if we failed to connect to the ws url (ECONNREFUSED/ECONNRESET) + // Indicates if we failed to connect to the ws url private failedToConnectDueToNetworkError = false; private readonly sendQueue = new AsyncQueue(); @@ -791,7 +792,7 @@ export class WebSocketShard extends AsyncEventEmitter { } private onError(error: Error) { - if ('code' in error && ['ECONNRESET', 'ECONNREFUSED'].includes(error.code as string)) { + if ('code' in error && KnownNetworkErrorCodes.has(error.code as string)) { this.debug(['Failed to connect to the gateway URL specified due to a network error']); this.failedToConnectDueToNetworkError = true; return;