From 4422dda00743076ebb336112a9cb530254c841e5 Mon Sep 17 00:00:00 2001 From: Timo Stamm Date: Tue, 3 Sep 2024 09:12:41 +0200 Subject: [PATCH] Fix flaky decompression error code (#1204) --- packages/connect-node/src/compression.ts | 37 ++++++++++-------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/packages/connect-node/src/compression.ts b/packages/connect-node/src/compression.ts index 242b26565..d30ddc64f 100644 --- a/packages/connect-node/src/compression.ts +++ b/packages/connect-node/src/compression.ts @@ -76,35 +76,30 @@ function wrapZLibErrors( readMaxBytes: number, ): Promise { return promise.catch((e) => { - const { code } = getNodeErrorProps(e); + const props = getNodeErrorProps(e); + let code = Code.Internal; + let message = "decompression failed"; // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check - switch (code) { + switch (props.code) { case "ERR_BUFFER_TOO_LARGE": - e = new ConnectError( - `message is larger than configured readMaxBytes ${readMaxBytes} after decompression`, - Code.ResourceExhausted, - ); + code = Code.ResourceExhausted; + message = `message is larger than configured readMaxBytes ${readMaxBytes} after decompression`; break; case "Z_DATA_ERROR": case "ERR_PADDING_2": - e = new ConnectError( - "decompression failed", - Code.InvalidArgument, - undefined, - undefined, - e, - ); + code = Code.InvalidArgument; break; default: - e = new ConnectError( - "decompression failed", - Code.Internal, - undefined, - undefined, - e, - ); + if ( + props.code !== undefined && + props.code.startsWith("ERR__ERROR_FORMAT_") + ) { + code = Code.InvalidArgument; + } break; } - return Promise.reject(e); + return Promise.reject( + new ConnectError(message, code, undefined, undefined, e), + ); }); }