Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(WebSocketShard): don't await #destroy in error bubbling logic #9276

Merged
merged 4 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/ws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) => {
Expand Down
10 changes: 6 additions & 4 deletions packages/ws/src/ws/WebSocketShard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down