Skip to content

Commit a61a832

Browse files
committed
chore(WebSocketShard): improve zlib-sync errors
1 parent 4d19426 commit a61a832

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

packages/ws/src/ws/WebSocketShard.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ export enum WebSocketShardDestroyRecovery {
5959
Resume,
6060
}
6161

62+
enum ZlibErrorCodes {
63+
Z_NEED_DICT = 2,
64+
Z_STREAM_END = 1,
65+
Z_ERRNO = -1,
66+
Z_STREAM_ERROR = -2,
67+
Z_DATA_ERROR = -3,
68+
Z_MEM_ERROR = -4,
69+
Z_BUF_ERROR = -5,
70+
Z_VERSION_ERROR = -6,
71+
}
72+
6273
export interface WebSocketShardEventsMap {
6374
[WebSocketShardEvents.Closed]: [code: number];
6475
[WebSocketShardEvents.Debug]: [message: string];
@@ -667,10 +678,11 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
667678
this.zLibSyncInflate.push(Buffer.from(decompressable), flush ? zLibSync.Z_SYNC_FLUSH : zLibSync.Z_NO_FLUSH);
668679

669680
if (this.zLibSyncInflate.err) {
670-
this.emit(
671-
WebSocketShardEvents.Error,
672-
new Error(`${this.zLibSyncInflate.err}${this.zLibSyncInflate.msg ? `: ${this.zLibSyncInflate.msg}` : ''}`),
673-
);
681+
// Try to match nodejs zlib errors as much as possible
682+
const error: NodeJS.ErrnoException = new Error(this.zLibSyncInflate.msg ?? undefined);
683+
error.errno = this.zLibSyncInflate.err;
684+
error.code = ZlibErrorCodes[this.zLibSyncInflate.err];
685+
this.emit(WebSocketShardEvents.Error, error);
674686
}
675687

676688
if (!flush) {

0 commit comments

Comments
 (0)