fix(DJSError): error code validation (#8149)

This commit is contained in:
Almeida
2022-06-22 19:33:47 +01:00
committed by GitHub
parent a3799f9ebb
commit 31f658247f
3 changed files with 12 additions and 18 deletions

View File

@@ -22,13 +22,14 @@ const BeforeReadyWhitelist = [
GatewayDispatchEvents.GuildMemberRemove, GatewayDispatchEvents.GuildMemberRemove,
]; ];
const UNRECOVERABLE_CLOSE_CODES = [ const unrecoverableErrorCodeMap = {
GatewayCloseCodes.AuthenticationFailed, [GatewayCloseCodes.AuthenticationFailed]: ErrorCodes.TokenInvalid,
GatewayCloseCodes.InvalidShard, [GatewayCloseCodes.InvalidShard]: ErrorCodes.ShardingInvalid,
GatewayCloseCodes.ShardingRequired, [GatewayCloseCodes.ShardingRequired]: ErrorCodes.ShardingRequired,
GatewayCloseCodes.InvalidIntents, [GatewayCloseCodes.InvalidIntents]: ErrorCodes.InvalidIntents,
GatewayCloseCodes.DisallowedIntents, [GatewayCloseCodes.DisallowedIntents]: ErrorCodes.DisallowedIntents,
]; };
const UNRESUMABLE_CLOSE_CODES = [1000, GatewayCloseCodes.AlreadyAuthenticated, GatewayCloseCodes.InvalidSeq]; const UNRESUMABLE_CLOSE_CODES = [1000, GatewayCloseCodes.AlreadyAuthenticated, GatewayCloseCodes.InvalidSeq];
/** /**
@@ -194,7 +195,7 @@ class WebSocketManager extends EventEmitter {
}); });
shard.on(ShardEvents.Close, event => { shard.on(ShardEvents.Close, event => {
if (event.code === 1_000 ? this.destroyed : UNRECOVERABLE_CLOSE_CODES.includes(event.code)) { if (event.code === 1_000 ? this.destroyed : event.code in unrecoverableErrorCodeMap) {
/** /**
* Emitted when a shard's WebSocket disconnects and will no longer reconnect. * Emitted when a shard's WebSocket disconnects and will no longer reconnect.
* @event Client#shardDisconnect * @event Client#shardDisconnect
@@ -245,8 +246,8 @@ class WebSocketManager extends EventEmitter {
try { try {
await shard.connect(); await shard.connect();
} catch (error) { } catch (error) {
if (error?.code && UNRECOVERABLE_CLOSE_CODES.includes(error.code)) { if (error?.code && error.code in unrecoverableErrorCodeMap) {
throw new Error(GatewayCloseCodes[error.code]); throw new Error(unrecoverableErrorCodeMap[error.code]);
// Undefined if session is invalid, error event for regular closes // Undefined if session is invalid, error event for regular closes
} else if (!error || error.code) { } else if (!error || error.code) {
this.debug('Failed to connect to the gateway, requeueing...', shard); this.debug('Failed to connect to the gateway, requeueing...', shard);

View File

@@ -16,7 +16,7 @@ function makeDiscordjsError(Base) {
constructor(code, ...args) { constructor(code, ...args) {
super(message(code, args)); super(message(code, args));
this[kCode] = code; this[kCode] = code;
if (Error.captureStackTrace) Error.captureStackTrace(this, DiscordjsError); Error.captureStackTrace?.(this, DiscordjsError);
} }
get name() { get name() {

View File

@@ -159,11 +159,4 @@ const Messages = {
[DjsErrorCodes.SweepFilterReturn]: 'The return value of the sweepFilter function was not false or a Function', [DjsErrorCodes.SweepFilterReturn]: 'The return value of the sweepFilter function was not false or a Function',
}; };
// Magic needed by WS
Messages.AuthenticationFailed = Messages[DjsErrorCodes.TokenInvalid];
Messages.InvalidShard = Messages[DjsErrorCodes.ShardingInvalid];
Messages.ShardingRequired = Messages[DjsErrorCodes.ShardingRequired];
Messages.InvalidIntents = Messages[DjsErrorCodes.InvalidIntents];
Messages.DisallowedIntents = Messages[DjsErrorCodes.DisallowedIntents];
module.exports = Messages; module.exports = Messages;