Skip to content

Commit 4dbeed9

Browse files
LJNeonalmeidx
andauthored
chore(WebSocketShard): improve zlib-sync errors (discordjs#10808)
* chore(WebSocketShard): improve zlib-sync errors * chore(WebSocketShard): move zlib-sync error numbers to constants file * chore(WebSocketShard): move enum, zlib-sync is lazily loaded --------- Co-authored-by: Almeida <github@almeidx.dev>
1 parent 02fbb70 commit 4dbeed9

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

packages/ws/src/ws/WebSocketShard.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -668,10 +668,23 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
668668
this.zLibSyncInflate.push(Buffer.from(decompressable), flush ? zLibSync.Z_SYNC_FLUSH : zLibSync.Z_NO_FLUSH);
669669

670670
if (this.zLibSyncInflate.err) {
671-
this.emit(
672-
WebSocketShardEvents.Error,
673-
new Error(`${this.zLibSyncInflate.err}${this.zLibSyncInflate.msg ? `: ${this.zLibSyncInflate.msg}` : ''}`),
674-
);
671+
// Must be here because zlib-sync is lazily loaded
672+
const ZlibErrorCodes = {
673+
[zLibSync.Z_NEED_DICT]: 'Z_NEED_DICT',
674+
[zLibSync.Z_STREAM_END]: 'Z_STREAM_END',
675+
[zLibSync.Z_ERRNO]: 'Z_ERRNO',
676+
[zLibSync.Z_STREAM_ERROR]: 'Z_STREAM_ERROR',
677+
[zLibSync.Z_DATA_ERROR]: 'Z_DATA_ERROR',
678+
[zLibSync.Z_MEM_ERROR]: 'Z_MEM_ERROR',
679+
[zLibSync.Z_BUF_ERROR]: 'Z_BUF_ERROR',
680+
[zLibSync.Z_VERSION_ERROR]: 'Z_VERSION_ERROR',
681+
} as const satisfies Record<number, string>;
682+
683+
// Try to match nodejs zlib errors as much as possible
684+
const error: NodeJS.ErrnoException = new Error(this.zLibSyncInflate.msg ?? undefined);
685+
error.errno = this.zLibSyncInflate.err;
686+
error.code = ZlibErrorCodes[this.zLibSyncInflate.err];
687+
this.emit(WebSocketShardEvents.Error, error);
675688
}
676689

677690
if (!flush) {

0 commit comments

Comments
 (0)