{"record":{"id":"78a40a24a9f2b7cb","repo":"oven-sh/bun","slug":"compressionfailed","errorCode":"CompressionFailed","errorMessage":"CompressionFailed","messagePattern":"CompressionFailed","errorType":"error_code","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/http/error.rs","lineNumber":4,"sourceCode":"#[allow(non_camel_case_types)]\n#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]\npub enum Error {\n    #[error(\"CompressionFailed\")]\n    CompressionFailed,\n    #[error(\"Aborted\")]\n    Aborted,\n    #[error(\"WriteFailed\")]\n    WriteFailed,\n    #[error(\"HTTP2RefusedStream\")]\n    HTTP2RefusedStream,\n    #[error(\"HTTP2ContentLengthMismatch\")]\n    HTTP2ContentLengthMismatch,\n    #[error(\"HTTP2FrameSizeError\")]\n    HTTP2FrameSizeError,\n    #[error(\"HTTP2ProtocolError\")]\n    HTTP2ProtocolError,\n    #[error(\"HTTP2FlowControlError\")]\n    HTTP2FlowControlError,\n    #[error(\"HTTP2EnhanceYourCalm\")]\n    HTTP2EnhanceYourCalm,\n    #[error(\"HTTP2HeaderListTooLarge\")]","sourceCodeStart":1,"sourceCodeEnd":22,"githubUrl":"https://github.com/oven-sh/bun/blob/8c5296ac459e8252d3cd702f3fbcbb0c249d95d5/src/http/error.rs#L1-L22","documentation":"Compressing a buffered fetch request body failed at the codec level (src/http/compress_body.rs — construction sites at :145, :168, :208, :221, :226, :238). This is the automatic request-body compression behind `fetch(url, { compress: ... })`: gzip/deflate go through libdeflate with a zlib streaming fallback, brotli/zstd use one-shot encoders. Any encoder init failure, bad zlib return code, zstd compress_bound error, or failed one-shot encode maps to CompressionFailed and fails the request.","triggerScenarios":"Calling fetch with `compress: { encoding: 'gzip'|'deflate'|'br'|'zstd', level }` where level is outside the codec's accepted range (zlib path clamps to 0..=9, but a wildly invalid value or mismatched encoding/level combos can still fail encoder creation), or the input triggers a compress_bound overflow (multi-GB zstd bodies). Buffered (string/Bytes) bodies only — streams skip compression.","commonSituations":"Porting Node code with numeric compression levels tuned per codec (brotli 11 passed where zlib 9 max); sending very large (>2-4GB) JSON payloads with compress on; version skew where a codec level enum changed meaning.","solutions":["Drop the custom `level` and use `compress: true` or `compress: 'gzip'` — defaults are pre-validated per codec (deflate 6, brotli 6, zstd 3).","Clamp the level yourself per encoding: deflate/gzip 0-9, brotli 0-11, zstd 1-22 minus extreme values.","For multi-GB bodies, split the upload or compress manually with Bun.gzip/Bun.deflateSync in chunks instead of request-body compression.","Catch the fetch rejection and retry uncompressed if compression is optional for the server."],"exampleFix":"// before\nawait fetch(url, { method: 'POST', body: bigJson, compress: { type: 'gzip', level: 11 } }); // zlib max is 9\n// after\nawait fetch(url, { method: 'POST', body: bigJson, compress: { type: 'gzip', level: Math.min(9, level) } });\n// or simply\nawait fetch(url, { method: 'POST', body: bigJson, compress: true });","handlingStrategy":"try-catch","validationCode":"const LEVELS = { gzip: [0, 9], deflate: [0, 9], br: [0, 11], zstd: [1, 22] };\nfunction validCompress(opt) {\n  if (opt === true || typeof opt === 'string') return true;\n  const enc = opt.type ?? opt.encoding;\n  const [lo, hi] = LEVELS[enc];\n  return opt.level === undefined || (opt.level >= lo && opt.level <= hi);\n}\nif (!validCompress(opts.compress)) opts.compress = true; // fall back to defaults","typeGuard":null,"tryCatchPattern":"try {\n  const res = await fetch(url, { method: 'POST', body, compress });\n} catch (err) {\n  if (String(err.message).includes('CompressionFailed')) {\n    return fetch(url, { method: 'POST', body }); // retry uncompressed\n  }\n  throw err;\n}","preventionTips":["Use `compress: true` — per-codec defaults are pre-validated","Clamp custom levels per encoding (deflate max 9, not brotli's 11)","For multi-GB bodies, compress manually or chunk instead of using request-body compression"],"tags":["http","fetch","compression","request-body","network"],"backgroundTag":null,"analyzedSha":"8c5296ac459e8252d3cd702f3fbcbb0c249d95d5","analyzedAt":"2026-08-16T08:01:58.794Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}