{"record":{"id":"2ecb5a94dae9f654","repo":"can1357/oh-my-pi","slug":"resizeimage-failed-to-decode-image-and-cannot-hon","errorCode":null,"errorMessage":"resizeImage: failed to decode image and cannot honor excludeWebP for a WebP source","messagePattern":"resizeImage: failed to decode image and cannot honor excludeWebP for a WebP source","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/utils/image-resize.ts","lineNumber":388,"sourceCode":"\t\t\tmimeType: best.mimeType,\n\t\t\toriginalWidth,\n\t\t\toriginalHeight,\n\t\t\twidth: finalWidth,\n\t\t\theight: finalHeight,\n\t\t\twasResized: true,\n\t\t\tget data() {\n\t\t\t\treturn Buffer.from(best.buffer).toBase64();\n\t\t\t},\n\t\t};\n\t} catch {\n\t\tconst headerDimensions = readImageHeaderDimensions(inputBuffer);\n\t\tconst fallbackMimeType = img.mimeType ?? headerDimensions?.mimeType ?? \"application/octet-stream\";\n\t\t// Bun.Image rejected the input — we cannot decode/re-encode it.\n\t\t// When the caller demanded WebP exclusion AND the source might be WebP,\n\t\t// returning the original buffer would silently violate that contract,\n\t\t// so surface an explicit error instead.\n\t\tif (excludeWebP && (fallbackMimeType === \"image/webp\" || (!img.mimeType && !headerDimensions))) {\n\t\t\tthrow new Error(\"resizeImage: failed to decode image and cannot honor excludeWebP for a WebP source\");\n\t\t}\n\t\treturn {\n\t\t\tbuffer: inputBuffer,\n\t\t\tmimeType: fallbackMimeType,\n\t\t\toriginalWidth: headerDimensions?.width ?? 0,\n\t\t\toriginalHeight: headerDimensions?.height ?? 0,\n\t\t\twidth: headerDimensions?.width ?? 0,\n\t\t\theight: headerDimensions?.height ?? 0,\n\t\t\twasResized: false,\n\t\t\tdecodeFailed: true,\n\t\t\tget data() {\n\t\t\t\treturn img.data;\n\t\t\t},\n\t\t};\n\t}\n}\n\n/**","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/utils/image-resize.ts#L370-L406","documentation":"resizeImage falls back to returning the original buffer when Bun.Image cannot decode the input — but if the caller set excludeWebP and the source is (or might be) WebP, honoring that contract is impossible without decoding, so it throws explicitly rather than silently returning WebP data.","triggerScenarios":"Calling resizeImage (directly or via loadInMemoryImageInput with excludeWebP:true) with data Bun.Image fails to decode, where the declared mimeType is image/webp or the format could not be identified from headers either.","commonSituations":"Providers reject WebP so callers pass excludeWebP; source file is corrupt or truncated WebP; file has unknown format and undecodable bytes; exotic codecs (HEIC, AVIF) that cannot be transcoded.","solutions":["Repair or re-export the image (convert to PNG/JPEG externally) so decoding succeeds.","Drop the excludeWebP flag if returning the original (possibly WebP) buffer is acceptable.","Verify the file is not truncated or corrupt — decode it in an image viewer or tool.","Pre-detect the format with a header check and transcode non-PNG/JPEG sources before calling resizeImage."],"exampleFix":"// before: await resizeImage({ data: webpB64, mimeType: \"image/webp\" }, { excludeWebP: true }); // corrupt webp  // after: transcode/repair externally (e.g. magick corrupt.webp out.png), then resizeImage with mimeType image/png","handlingStrategy":"try-catch","validationCode":"function looksWebp(buf: Uint8Array): boolean { return buf[0] === 0x52 && buf[1] === 0x49 && buf[8] === 0x57 && buf[9] === 0x45; } if (excludeWebP && looksWebp(sourceBytes)) { await transcodeToPng(source); }","typeGuard":"function looksWebp(buf: Uint8Array): boolean { return buf.length > 12 && buf[0] === 0x52 && buf[1] === 0x49 && buf[8] === 0x57 && buf[9] === 0x45; } // RIFF....WEBP","tryCatchPattern":"try { const resized = await resizeImage(img, { excludeWebP: true }); } catch (err) { if (err instanceof Error && err.message.includes(\"cannot honor excludeWebP\")) { const png = await externalTranscode(source, \"png\"); return loadImageInput(png); } throw err; }","preventionTips":["Verify source integrity (magic bytes, decodability) before passing excludeWebP.","Only set excludeWebP when the provider truly rejects WebP and your pipeline can transcode.","Keep an external transcoder (sharp/magick) for sources Bun.Image cannot decode.","Pre-convert WebP/HEIC/AVIF sources to PNG or JPEG at ingestion."],"tags":["image","webp","decoding"],"backgroundTag":"image-decode-failed","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}