{"id":"6c4cb38364f76b0b","repo":"gofiber/fiber","slug":"failed-to-create-directory-w","errorCode":null,"errorMessage":"failed to create directory: %w","messagePattern":"failed to create directory: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client/response.go","lineNumber":157,"sourceCode":"\n// Save writes the response body to a file or io.Writer.\n// If a string path is provided, it creates directories if needed, then writes to a file.\n// If an io.Writer is provided, it writes directly to it.\n// When streaming is enabled, the body is read directly from the stream.\nfunc (r *Response) Save(v any) error {\n\tswitch p := v.(type) {\n\tcase string:\n\t\tfile := filepath.Clean(p)\n\t\tdir := filepath.Dir(file)\n\n\t\t// Create directory if it doesn't exist\n\t\tif _, err := os.Stat(dir); err != nil {\n\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:","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/client/response.go#L139-L175","documentation":"Returned by Response.Save (client/response.go:157) when os.MkdirAll fails while creating the parent directory tree for the target file (created with mode 0o750). Save auto-creates missing directories; if creation fails (permission denied, read-only filesystem, invalid path) the error is wrapped here.","triggerScenarios":"Calling resp.Save(\"/new/nested/path/file\") where the parent directories do not exist and MkdirAll cannot create them — permission denied on an ancestor, a read-only filesystem, a path component that is a file rather than a directory, or a path that exceeds filesystem limits.","commonSituations":"App user lacks write permission on the base directory; running on a read-only root/container layer; a path that conflicts with an existing file (e.g. /tmp is a file); disk full; path too long.","solutions":["Grant the process write permission on the base directory or choose a writable location (e.g. an attached volume).","Pre-create the directory tree with correct ownership before calling Save.","Ensure no path component is an existing file.","Free disk space if MkdirAll fails due to ENOSPC."],"exampleFix":"// before — directory auto-create fails on read-only layer\nerr := resp.Save(\"/app/cache/resp.json\")\n\n// after — pre-create the dir on a writable volume with correct perms\nif err := os.MkdirAll(\"/data/cache\", 0o750); err != nil {\n    return fmt.Errorf(\"cannot create cache dir: %w\", err)\n}\nerr := resp.Save(\"/data/cache/resp.json\")","handlingStrategy":"validation","validationCode":"// Pre-create the output directory so Save does not have to.\nif err := os.MkdirAll(filepath.Dir(path), 0o750); err != nil {\n    return fmt.Errorf(\"cannot create output dir: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := resp.Save(path); err != nil {\n    if strings.Contains(err.Error(), \"failed to create directory\") {\n        // choose a writable fallback location and retry\n        path = filepath.Join(os.TempDir(), filepath.Base(path))\n        return resp.Save(path)\n    }\n    return err\n}","preventionTips":["Grant the process write permission on the base directory or use a writable volume.","Pre-create the directory tree with correct ownership at startup.","Ensure no path component is an existing file."],"tags":["client","save","filesystem","mkdir","permissions"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}