{"record":{"id":"81b9bfbc9a68df28","repo":"valyala/fasthttp","slug":"error-when-copying-form-file-q-q-w","errorCode":null,"errorMessage":"error when copying form file %q (%q): %w","messagePattern":"error when copying form file %q \\(%q\\): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"http.go","lineNumber":1255,"sourceCode":"\t\t\t\treturn fmt.Errorf(\"cannot write form field %q value %q: %w\", k, v, err)\n\t\t\t}\n\t\t}\n\t}\n\n\t// marshal files\n\tfor k, fvv := range f.File {\n\t\tfor _, fv := range fvv {\n\t\t\tvw, err := mw.CreatePart(fv.Header)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"cannot create form file %q (%q): %w\", k, fv.Filename, err)\n\t\t\t}\n\t\t\tfh, err := fv.Open()\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"cannot open form file %q (%q): %w\", k, fv.Filename, err)\n\t\t\t}\n\t\t\tif _, err = copyZeroAlloc(vw, fh); err != nil {\n\t\t\t\t_ = fh.Close()\n\t\t\t\treturn fmt.Errorf(\"error when copying form file %q (%q): %w\", k, fv.Filename, err)\n\t\t\t}\n\t\t\tif err = fh.Close(); err != nil {\n\t\t\t\treturn fmt.Errorf(\"cannot close form file %q (%q): %w\", k, fv.Filename, err)\n\t\t\t}\n\t\t}\n\t}\n\n\tif err := mw.Close(); err != nil {\n\t\treturn fmt.Errorf(\"error when closing multipart form writer: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc readMultipartForm(r io.Reader, boundary string, size, maxInMemoryFileSize int) (*multipart.Form, error) {\n\t// Do not care about memory allocations here, since they are tiny\n\t// compared to multipart data (aka multi-MB files) usually sent\n\t// in multipart/form-data requests.","sourceCodeStart":1237,"sourceCodeEnd":1273,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/http.go#L1237-L1273","documentation":"fasthttp wraps the error from copying the form file's contents into the multipart writer part (copyZeroAlloc) when building a multipart/form-data body. It reports which field/filename failed to stream. The file handle is closed before returning.","triggerScenarios":"Writing a request whose multipartForm contains a file field whose Open() reader returns an I/O error (e.g. file deleted after being registered via SetFormFileCreate, disk read failure, custom io.Reader erroring mid-copy).","commonSituations":"Registering an os.File with SetFormFileCreate and the file is removed/renamed before client.Do; reading from a failing network or pipe reader; permission denied on the underlying file at read time.","solutions":["Check the wrapped cause for the real I/O error (permission, file not found, etc.).","Ensure the file still exists and is readable at request time, not just at SetFormFile time.","If using a custom io.Reader, make sure it returns io.EOF instead of an error at end of stream.","Open the file fresh just before the request instead of holding a stale handle."],"exampleFix":"// before\nf, _ := os.Open(path) // opened early, may be deleted later\nreq.SetFormFileCreate(\"upload\", f.Name())\n// after\nif _, err := os.Stat(path); err != nil {\n    return fmt.Errorf(\"form file missing: %w\", err)\n}\nreq.SetFormFileCreate(\"upload\", path)\nif err := client.Do(req, resp); err != nil { ... }","handlingStrategy":"validation","validationCode":"f, err := os.Open(path)\nif err != nil { return err }\nif _, err := f.Stat(); err != nil { f.Close(); return err }\nf.Close()","typeGuard":"func isReadableFile(path string) bool {\n    f, err := os.Open(path)\n    if err != nil { return false }\n    f.Close()\n    return true\n}","tryCatchPattern":"if err := client.Do(req, resp); err != nil {\n    if strings.Contains(err.Error(), \"error when copying form file\") {\n        return fmt.Errorf(\"upload source failed: %w\", err)\n    }\n    return err\n}","preventionTips":["Verify files exist and are readable right before client.Do","Avoid custom readers that error at EOF; return io.EOF properly","Open files close to request time, not long before"],"tags":["multipart","io","file-read"],"backgroundTag":"io-error-during-stream-copy","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}