{"id":"ce61eb421f2c2b6c","repo":"gofiber/fiber","slug":"failed-to-write-response-body-to-writer-w","errorCode":null,"errorMessage":"failed to write response body to writer: %w","messagePattern":"failed to write response body to writer: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/response.go","lineNumber":178,"sourceCode":"\n\t\t// Create and write to file\n\t\toutFile, err := os.Create(file)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to create file: %w\", err)\n\t\t}\n\t\tdefer func() { _ = outFile.Close() }() //nolint:errcheck // not needed\n\n\t\t// Use BodyStream() which handles both streaming and non-streaming cases\n\t\tif _, err = io.Copy(outFile, r.BodyStream()); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to write response body to file: %w\", err)\n\t\t}\n\n\t\treturn nil\n\n\tcase io.Writer:\n\t\t// Use BodyStream() which handles both streaming and non-streaming cases\n\t\tif _, err := io.Copy(p, r.BodyStream()); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to write response body to writer: %w\", err)\n\t\t}\n\t\t// Close the writer if it implements io.WriteCloser\n\t\tif pc, ok := p.(io.WriteCloser); ok {\n\t\t\t_ = pc.Close() //nolint:errcheck // not needed\n\t\t}\n\n\t\treturn nil\n\n\tdefault:\n\t\treturn ErrNotSupportSaveMethod\n\t}\n}\n\n// Reset clears the Response object, making it ready for reuse.\nfunc (r *Response) Reset() {\n\tr.client = nil\n\tr.request = nil\n","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/client/response.go#L160-L196","documentation":"Returned by Response.Save (client/response.go:178) when io.Copy fails while streaming the response body into a caller-supplied io.Writer. Save accepts an io.Writer (e.g. *os.File, *bytes.Buffer, a network connection) and copies BodyStream() into it; any write error on the destination, or read error on the stream, is wrapped here.","triggerScenarios":"Calling resp.Save(writer) where writer is a network connection that closed, a pipe whose reader exited, a buffer that hit a hard cap, or any io.Writer whose Write returns an error. Also triggered when the body stream errors during the copy (server cut the connection, corrupt gzip).","commonSituations":"Streaming a download straight to an HTTP client whose connection dropped; piping through a writer whose downstream consumer died; a bounded buffer overflowing; proxying a large response that the next hop rejects mid-stream.","solutions":["Ensure the destination Writer's downstream stays alive for the full copy (keep the connection/reader open).","Use an unbounded or sufficiently large buffer if the destination can stall.","Inspect the wrapped error to distinguish a destination-write error from a source-stream error.","Retry the upstream request if the source stream broke."],"exampleFix":"// before — piping to a connection that may close mid-stream\nif err := resp.Save(downstreamConn); err != nil {\n    log.Print(err) // half-written downstream response\n}\n\n// after — buffer then forward, or handle the broken-pipe error explicitly\nvar buf bytes.Buffer\nif err := resp.Save(&buf); err != nil {\n    return fmt.Errorf(\"read response body: %w\", err)\n}\nif _, err := downstreamConn.Write(buf.Bytes()); err != nil {\n    return fmt.Errorf(\"forward body: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"// Ensure the destination implements io.Writer and is not prematurely closed.\nfunc liveWriter(w any) bool {\n    _, ok := w.(io.Writer)\n    return ok\n}","tryCatchPattern":"if err := resp.Save(w); err != nil {\n    if strings.Contains(err.Error(), \"write response body to writer\") {\n        // downstream writer failed — buffer and handle separately\n    }\n    return err\n}","preventionTips":["Keep the destination Writer's downstream alive for the full copy.","Use a sufficiently large buffer if the destination can stall.","Distinguish source-stream errors from destination-write errors via the wrapped error."],"tags":["client","save","io-writer","streaming","response"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}