{"record":{"id":"b51e0d7f5f4e4e63","repo":"OpenNHP/opennhp","slug":"failed-to-compress-response-body-w","errorCode":null,"errorMessage":"failed to compress response body: %w","messagePattern":"failed to compress response body: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"nhp/core/wasm/engine/host.go","lineNumber":61,"sourceCode":"\t}\n\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn nil, fmt.Errorf(\"unexpected status code: %d\", resp.StatusCode)\n\t}\n\n\tbody, err := io.ReadAll(resp.Body)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to read response body: %w\", err)\n\t}\n\n\tvar buf bytes.Buffer\n\tw := zlib.NewWriter(&buf)\n\t_, err = w.Write(body)\n\tw.Close()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to compress response body: %w\", err)\n\t}\n\n\tcompressedBody := buf.Bytes()\n\n\treturn compressedBody, nil\n}\n\nfunc GetEvidenceWithAgentUuid() ([]byte, error) {\n\tagentUniqueId, err := CalculateAgentUniqueId()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to get agent unique id: %v\", err)\n\t}\n\n\tevidence := map[string]any{\n\t\t\"test_purpose\":  \"this evidence is for testing purposes only\",\n\t\t\"measure\":       agentUniqueId,\n\t\t\"serial_number\": agentUniqueId,\n\t}","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/core/wasm/engine/host.go#L43-L79","documentation":"After reading the body, GetEvidenceWithCCUrl zlib-compresses it; this wraps a failure from w.Write or w.Close. Practically this only fires if the underlying bytes.Buffer write fails, which is nearly impossible — the error is defensive.","triggerScenarios":"The zlib Writer's Close returning an error (e.g. flushing internal deflate state failed), or Write to the in-memory buffer erroring; note the code ignores Write's error and only checks the one after Close.","commonSituations":"Essentially unreachable in normal operation; would indicate severe memory exhaustion or a Go runtime-level failure.","solutions":["Verify w.Write's error is checked separately from w.Close so failures are attributed correctly","If this fires, inspect system memory (bytes.Buffer growth) and Go runtime health","Treat as a bug report scenario — capture the wrapped error and open an issue"],"exampleFix":"// before\n_, err = w.Write(body)\nw.Close()\nif err != nil {\n    return nil, fmt.Errorf(\"failed to compress response body: %w\", err)\n}\n// after\nif _, err = w.Write(body); err != nil {\n    return nil, fmt.Errorf(\"failed to compress response body: %w\", err)\n}\nif err = w.Close(); err != nil {\n    return nil, fmt.Errorf(\"failed to close zlib writer: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"ev, err := engine.GetEvidenceWithCCUrl()\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to compress response body\") {\n        // in-memory compression failed; report bug, retry rarely helps\n        return fmt.Errorf(\"evidence compression failed: %w\", err)\n    }\n}","preventionTips":["Check both w.Write and w.Close errors separately","Keep in-memory buffers modest for large payloads","Treat occurrences as Go runtime/memory anomalies worth reporting"],"tags":["zlib","compression","defensive"],"backgroundTag":"internal-invariant-violation","analyzedSha":"6e04ca5ff03222a699c24205cd4bf8fee9af7ffe","analyzedAt":"2026-09-07T15:44:59.941Z","contentChangedAt":"2026-09-07T15:44:59.941Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}