{"id":"053af4714dc58fe9","repo":"gofiber/fiber","slug":"create-file-error-w","errorCode":null,"errorMessage":"create file error: %w","messagePattern":"create file error: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/hooks.go","lineNumber":318,"sourceCode":"}\n\nfunc addFormFile(mw *multipart.Writer, f *File, fileBuf *[]byte) error {\n\t// If reader is not set, open the file.\n\tif f.reader == nil {\n\t\tvar err error\n\t\tf.reader, err = os.Open(f.path)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"open file error: %w\", err)\n\t\t}\n\t}\n\n\t// Ensure the file reader is always closed after copying.\n\tdefer f.reader.Close() //nolint:errcheck // not needed\n\n\t// Create form file and copy the content.\n\tw, err := mw.CreateFormFile(f.fieldName, f.name)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"create file error: %w\", err)\n\t}\n\n\tif _, err := io.CopyBuffer(w, f.reader, *fileBuf); err != nil {\n\t\treturn fmt.Errorf(\"failed to copy file data: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// parserResponseCookie parses the Set-Cookie headers from the response and stores them.\nfunc parserResponseCookie(c *Client, resp *Response, req *Request) error {\n\tvar err error\n\tfor key, value := range resp.RawResponse.Header.Cookies() {\n\t\tcookie := fasthttp.AcquireCookie()\n\t\tif err = cookie.ParseBytes(value); err != nil {\n\t\t\tfasthttp.ReleaseCookie(cookie)\n\t\t\tbreak\n\t\t}","sourceCodeStart":300,"sourceCodeEnd":336,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/client/hooks.go#L300-L336","documentation":"Returned in addFormFile (client/hooks.go:318) when multipart.Writer.CreateFormFile fails while building a file part. CreateFormFile writes the part headers (Content-Disposition and Content-Type: application/octet-stream) into the body writer; failure indicates the underlying writer rejected the header write or the field/file name is invalid.","triggerScenarios":"The body writer errors while emitting the part header (buffer overflow, transport teardown), or the fieldName/name arguments produce an invalid header. Occurs during body construction of a multipart upload after the file has been successfully opened.","commonSituations":"Uploading with a very long filename or field name that overflows the request buffer; a body-stream destination closed mid-build; concurrent reuse/release of the Request; an internal fasthttp request buffer limit being exceeded.","solutions":["Keep field names and filenames short and free of CR/LF characters.","Avoid releasing/reusing the Request concurrently with body building.","If the body is large, confirm it fits the configured request size or use streaming.","Inspect the wrapped error to determine whether it is a size-limit or connection-level failure."],"exampleFix":"// before — very long / unsafe filename\nreq.SetFiles(\"/tmp/\" + strings.Repeat(\"a\", 500) + \".bin\")\n\n// after — short, safe filename\nreq.SetFiles(\"/tmp/upload.bin\")","handlingStrategy":"validation","validationCode":"// Ensure field/file names are short and free of CR/LF.\nvar safeName = regexp.MustCompile(`^[\\x20-\\x7e]{1,128}$`)\nfunc safePartName(s string) bool {\n    if strings.ContainsAny(s, \"\\r\\n\") { return false }\n    return safeName.MatchString(s)\n}","typeGuard":"func safePartName(s string) bool {\n    return !strings.ContainsAny(s, \"\\r\\n\") && safeName.MatchString(s)\n}","tryCatchPattern":null,"preventionTips":["Keep multipart field names and filenames short and free of control characters.","Avoid concurrent release/reuse of the Request during body construction.","Confirm the request body size accommodates the part headers."],"tags":["client","multipart","upload","header"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}