{"record":{"id":"159a8d592645d8cc","repo":"d2lang/d2","slug":"failed-to-decode-q-response-for-s-w","errorCode":null,"errorMessage":"failed to decode %q response for %s: %w","messagePattern":"failed to decode %q response for (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/imgbundler/imgbundler.go","lineNumber":245,"sourceCode":"\tif err != nil {\n\t\treturn nil, \"\", err\n\t}\n\tdefer resp.Body.Close()\n\tl.Debug(fmt.Sprintf(\"fetched %s remotely - response code %v\", string(href), resp.StatusCode))\n\tif resp.StatusCode != 200 {\n\t\treturn nil, \"\", fmt.Errorf(\"expected status 200 but got %d %s\", resp.StatusCode, resp.Status)\n\t}\n\tr := http.MaxBytesReader(nil, resp.Body, maxImageSize)\n\tbuf, err := io.ReadAll(r)\n\tif err != nil {\n\t\treturn nil, \"\", err\n\t}\n\tcontentType := resp.Header.Get(\"Content-Type\")\n\tcontentEncoding := resp.Header.Get(\"Content-Encoding\")\n\tif contentEncoding != \"\" {\n\t\tbuf, err = decodeContentEncoding(buf, contentEncoding)\n\t\tif err != nil {\n\t\t\treturn nil, \"\", fmt.Errorf(\"failed to decode %q response for %s: %w\", contentEncoding, href, err)\n\t\t}\n\t}\n\tl.Debug(fmt.Sprintf(\"fetched content type: %s, Content length: %d bytes\", contentType, len(buf)))\n\n\treturn buf, contentType, nil\n}\n\nfunc decodeContentEncoding(buf []byte, contentEncoding string) ([]byte, error) {\n\tencodings := strings.Split(contentEncoding, \",\")\n\tfor i := len(encodings) - 1; i >= 0; i-- {\n\t\tencoding := strings.TrimSpace(strings.ToLower(encodings[i]))\n\t\tif encoding == \"\" || encoding == \"identity\" {\n\t\t\tcontinue\n\t\t}\n\n\t\tvar err error\n\t\tswitch encoding {\n\t\tcase \"gzip\", \"x-gzip\":","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/d2lang/d2/blob/0d69dca6f532ceaeacd615d35d1eaa41a238ffdb/lib/imgbundler/imgbundler.go#L227-L263","documentation":"httpGet reads the response body and, if the server declared a Content-Encoding, decodes it (gzip, br, deflate). If decodeContentEncoding fails, this error wraps the underlying decode failure, naming the encoding and the URL. It means the body bytes do not match the advertised Content-Encoding header.","triggerScenarios":"Server sends Content-Encoding: gzip/br/deflate but the body is not valid data in that encoding — e.g. a proxy double-encoded or stripped the encoding, a broken/buggy origin, an antivirus middlebox rewriting bodies, or truncated compressed bodies.","commonSituations":"Corporate proxies and TLS-terminating LBs mangling compression, CDNs serving pre-compressed assets with wrong headers, or misconfigured web servers setting the header but sending identity content.","solutions":["Inspect the raw response with curl --compressed to see whether the body actually matches the declared Content-Encoding","Fix or bypass the misbehaving proxy/CDN layer that rewrites the body","Retry the request — a truncated transfer often resolves on re-fetch","If you control the origin, correct the Content-Encoding header or disable compression for the asset"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Probe that the body decodes as advertised\nresp, _ := http.Get(u)\nenc := resp.Header.Get(\"Content-Encoding\")\nbody, _ := io.ReadAll(resp.Body)\nvar probe interface{ Read([]byte) (int, error) }\nswitch enc {\ncase \"\", \"identity\":\n\tprobe = bytes.NewReader(body)\ncase \"gzip\":\n\tprobe, _ = gzip.NewReader(bytes.NewReader(body))\ndefault:\n\treturn fmt.Errorf(\"unsupported probe encoding %q\", enc)\n}","typeGuard":null,"tryCatchPattern":"if _, _, err := httpGet(ctx, client, href); err != nil {\n\tvar decodeErr = \"failed to decode\"\n\tif strings.Contains(err.Error(), decodeErr) {\n\t\t// retry without compression or log and skip this asset\n\t}\n}","preventionTips":["Test fetch paths through any corporate proxy with curl --compressed","Keep Accept-Encoding limited to encodings you can decode","Watch for middleboxes/AV that rewrite compressed responses","Retry once without compression when decode fails"],"tags":["http","compression","gzip","brotli","go"],"backgroundTag":"content-decoding-failed","analyzedSha":"0d69dca6f532ceaeacd615d35d1eaa41a238ffdb","analyzedAt":"2026-08-31T12:19:21.182Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}