{"id":"f722538c35e2f63e","repo":"gofiber/fiber","slug":"open-file-error-w","errorCode":null,"errorMessage":"open file error: %w","messagePattern":"open file error: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/hooks.go","lineNumber":308,"sourceCode":"\t\tif f.fieldName == \"\" {\n\t\t\tf.fieldName = \"file\" + strconv.Itoa(i+1)\n\t\t}\n\n\t\tif err := addFormFile(mw, f, fileBuf); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\treturn nil\n}\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}","sourceCodeStart":290,"sourceCodeEnd":326,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/client/hooks.go#L290-L326","documentation":"Returned in addFormFile (client/hooks.go:308) when os.Open(f.path) fails for a file added via Request.SetFiles (path-based, no reader). The client opens each file lazily at body-build time; any open error — missing file, permission denied, path too long — is wrapped here.","triggerScenarios":"Calling Request.SetFiles(\"/nonexistent/path\") or with a path the process cannot read; a relative path resolved from an unexpected working directory; a file deleted between adding it and sending the request; insufficient filesystem permissions.","commonSituations":"Deploying with a file path that exists in dev but not in the container; relative paths that break under a different CWD; tempfile already cleaned up; permission mismatch between the app user and the file owner; path from user input that points outside allowed dirs.","solutions":["Verify the file exists and is readable (os.Stat / os.Open) before calling SetFiles.","Use absolute paths or resolve relative paths against a known base directory.","Ensure the process user has read permission on the file.","If the file is transient, use SetFileReader with an in-memory/stream reader so no filesystem open is needed."],"exampleFix":"// before — relative path that may not resolve\nreq.SetFiles(\"./uploads/owner.pdf\")\n\n// after — absolute path validated up front\nabs, _ := filepath.Abs(\"./uploads/owner.pdf\")\nif _, err := os.Stat(abs); err != nil {\n    return fmt.Errorf(\"upload file missing: %w\", err)\n}\nreq.SetFiles(abs)","handlingStrategy":"validation","validationCode":"// Verify a file is readable before adding it to the upload.\nfunc readableFile(path string) error {\n    f, err := os.Open(path)\n    if err != nil {\n        return err\n    }\n    f.Close()\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if _, err := os.Stat(path); err != nil {\n    return fmt.Errorf(\"upload file not available: %w\", err)\n}\nreq.SetFiles(path)","preventionTips":["Use absolute paths resolved against a known base directory.","Stat files before calling SetFiles to fail with a clear message.","Ensure the process user has read permission on upload files.","For transient files, prefer SetFileReader to avoid filesystem dependency."],"tags":["client","upload","filesystem","permissions"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}