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,
];
const UNRECOVERABLE_CLOSE_CODES = [
GatewayCloseCodes.AuthenticationFailed,
GatewayCloseCodes.InvalidShard,
GatewayCloseCodes.ShardingRequired,
GatewayCloseCodes.InvalidIntents,
GatewayCloseCodes.DisallowedIntents,
];
const unrecoverableErrorCodeMap = {
[GatewayCloseCodes.AuthenticationFailed]: ErrorCodes.TokenInvalid,
[GatewayCloseCodes.InvalidShard]: ErrorCodes.ShardingInvalid,
[GatewayCloseCodes.ShardingRequired]: ErrorCodes.ShardingRequired,
[GatewayCloseCodes.InvalidIntents]: ErrorCodes.InvalidIntents,
[GatewayCloseCodes.DisallowedIntents]: ErrorCodes.DisallowedIntents,
};
const UNRESUMABLE_CLOSE_CODES = [1000, GatewayCloseCodes.AlreadyAuthenticated, GatewayCloseCodes.InvalidSeq];
/**
@@ -194,7 +195,7 @@ class WebSocketManager extends EventEmitter {
});
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.
* @event Client#shardDisconnect
@@ -245,8 +246,8 @@ class WebSocketManager extends EventEmitter {
try {
await shard.connect();
} catch (error) {
if (error?.code && UNRECOVERABLE_CLOSE_CODES.includes(error.code)) {
throw new Error(GatewayCloseCodes[error.code]);
if (error?.code && error.code in unrecoverableErrorCodeMap) {
throw new Error(unrecoverableErrorCodeMap[error.code]);
// Undefined if session is invalid, error event for regular closes
} else if (!error || error.code) {
this.debug('Failed to connect to the gateway, requeueing...', shard);

View File

@@ -16,7 +16,7 @@ function makeDiscordjsError(Base) {
constructor(code, ...args) {
super(message(code, args));
this[kCode] = code;
if (Error.captureStackTrace) Error.captureStackTrace(this, DiscordjsError);
Error.captureStackTrace?.(this, DiscordjsError);
}
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',
};
// 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;