mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
fix(WebSocketShard): don't await #destroy in error bubbling logic (#9276)
* fix(WebSocketShard): don't await #destroy in error bubbling logic * fix: properly throw abort errors * chore: vlad's nit --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -39,7 +39,7 @@ pnpm add @discordjs/ws
|
||||
## Example usage
|
||||
|
||||
```ts
|
||||
import { WebSocketManager, WebSocketShardEvents } from '@discordjs/ws';
|
||||
import { WebSocketManager, WebSocketShardEvents, CompressionMethod } from '@discordjs/ws';
|
||||
import { REST } from '@discordjs/rest';
|
||||
|
||||
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
|
||||
@@ -48,6 +48,8 @@ const manager = new WebSocketManager({
|
||||
token: process.env.DISCORD_TOKEN,
|
||||
intents: 0, // for no intents
|
||||
rest,
|
||||
// uncomment if you have zlib-sync installed and want to use compression
|
||||
// compression: CompressionMethod.ZlibStream,
|
||||
});
|
||||
|
||||
manager.on(WebSocketShardEvents.Dispatch, (event) => {
|
||||
|
||||
@@ -279,16 +279,18 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
|
||||
await promise;
|
||||
return { ok: true };
|
||||
} catch (error) {
|
||||
const isAbortError = error instanceof Error && error.name === 'AbortError';
|
||||
|
||||
// Any error that isn't an abort error would have been caused by us emitting an error event in the first place
|
||||
// See https://nodejs.org/api/events.html#eventsonceemitter-name-options for `once()` behavior
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
this.emit(WebSocketShardEvents.Error, { error });
|
||||
if (isAbortError) {
|
||||
this.emit(WebSocketShardEvents.Error, { error: error as Error });
|
||||
}
|
||||
|
||||
// As stated previously, any other error would have been caused by us emitting the error event, which looks
|
||||
// like { error: unknown }
|
||||
// eslint-disable-next-line no-ex-assign
|
||||
error = (error as { error: unknown }).error;
|
||||
error = error instanceof Error ? error : (error as { error: unknown }).error;
|
||||
|
||||
// If the user has no handling on their end (error event) simply throw.
|
||||
// We also want to throw if we're still in the initial `connect()` call, since that's the only time
|
||||
@@ -298,7 +300,7 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
|
||||
}
|
||||
|
||||
// If the error is handled, we can just try to reconnect
|
||||
await this.destroy({
|
||||
void this.destroy({
|
||||
code: CloseCodes.Normal,
|
||||
reason: 'Something timed out or went wrong while waiting for an event',
|
||||
recover: WebSocketShardDestroyRecovery.Reconnect,
|
||||
|
||||
Reference in New Issue
Block a user