{"id":"3968b08a50a191cd","repo":"gofiber/fiber","slug":"failed-to-write-response-body-to-file-w","errorCode":null,"errorMessage":"failed to write response body to file: %w","messagePattern":"failed to write response body to file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/response.go","lineNumber":170,"sourceCode":"\t\t\tif !errors.Is(err, fs.ErrNotExist) {\n\t\t\t\treturn fmt.Errorf(\"failed to check directory: %w\", err)\n\t\t\t}\n\n\t\t\tif err = os.MkdirAll(dir, 0o750); err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to create directory: %w\", err)\n\t\t\t}\n\t\t}\n\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","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/client/response.go#L152-L188","documentation":"Returned by Response.Save (client/response.go:170) when io.Copy fails while streaming the response body into the created file via BodyStream(). After the file is opened successfully, the copy can still fail mid-write — disk full, write I/O error, or the body stream itself errors during reading.","triggerScenarios":"Calling resp.Save(path) on a large response that fills the disk mid-copy, a body stream that errors partway (server closed connection, decompression error), or a filesystem write error (NFS failure, disk fault). The file may be left partially written.","commonSituations":"Disk full; network filesystem write failure; the server streams a huge body and the local disk cannot hold it; the response stream breaks mid-download; a decompressing BodyStream encounters corrupt compressed data.","solutions":["Ensure sufficient free disk space for the full response body before saving.","On error, remove the partial file so stale data is not consumed downstream.","For large downloads, check Content-Length and fail early if it exceeds available space.","Retry the request if the cause was a transient stream/network error."],"exampleFix":"// before — partial file left on failure\nif err := resp.Save(path); err != nil {\n    log.Print(err) // stale partial file remains\n}\n\n// after — clean up partial output on copy failure\nif err := resp.Save(path); err != nil {\n    _ = os.Remove(path)\n    return fmt.Errorf(\"save response: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"// Fail early if available disk space is less than Content-Length.\nfunc enoughSpace(path string, need int64) bool {\n    var st unix.Statfs_t\n    if unix.Statfs(path, &st) != nil { return true }\n    return int64(st.Bavail)*int64(st.Bsize) >= need\n}","typeGuard":null,"tryCatchPattern":"if err := resp.Save(path); err != nil {\n    _ = os.Remove(path) // discard partial file\n    if strings.Contains(err.Error(), \"write response body to file\") {\n        return fmt.Errorf(\"save failed (disk/stream): %w\", err)\n    }\n    return err\n}","preventionTips":["Check free disk space against Content-Length before saving large responses.","Remove partial files on copy failure to prevent stale data use.","Retry upstream if the stream broke mid-download."],"tags":["client","save","io-copy","disk-full","response"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}