{"record":{"id":"ece0f8d9a4e31cc9","repo":"gofr-dev/gofr","slug":"errfilenotopenforreading","errorCode":"errFileNotOpenForReading","errorMessage":"file not open for reading","messagePattern":"file not open for reading","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/datasource/file/common_file.go","lineNumber":18,"sourceCode":"package file\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"strings\"\n\t\"time\"\n\n\t\"gofr.dev/pkg/gofr/datasource\"\n)\n\n// Error variables for common file operations.\nvar (\n\t// errFileNotOpenForReading is returned when attempting to read from a file that wasn't opened for reading.\n\terrFileNotOpenForReading = errors.New(\"file not open for reading\")\n\n\t// errFileNotOpenForWriting is returned when attempting to write to a file that wasn't opened for writing.\n\terrFileNotOpenForWriting = errors.New(\"file not open for writing\")\n\n\t// errWriteAtNotSupported is returned when WriteAt is called on cloud storage.\n\terrWriteAtNotSupported = errors.New(\"WriteAt not supported for cloud storage\")\n\n\t// errWriterNil is returned when NewWriter returns nil.\n\terrWriterNil = errors.New(\"NewWriter returned nil\")\n)\n\n// File permission constants.\nconst (\n\t// DefaultFileMode is the standard file permission (0644 = rw-r--r--).\n\tDefaultFileMode os.FileMode = 0644\n\n\t// DefaultDirMode is the standard directory permission (0755 = rwxr-xr-x).\n\tDefaultDirMode os.FileMode = 0755","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/datasource/file/common_file.go#L1-L36","documentation":"A sentinel error from the common file layer: the file handle was opened without read intent (openFlags lacked read), so Read/ReadAll refuse to operate. It prevents silently reading an empty stream from a write-only handle.","triggerScenarios":"Calling Read or ReadAll on a file obtained via Create/AppendMode or opened with write-only flags in common_file.go, rather than via an Open/read-enabled handle.","commonSituations":"Opening a cloud file for append/write then trying to read back what was written without reopening; Copy-on-write patterns forgetting to reopen with read; misunderstanding that write-mode handles in this abstraction don't also grant read.","solutions":["Reopen the file in read mode (or read+write flags if supported by the adapter) before calling Read/ReadAll.","Check the file's open mode / read flag before reading, and fail fast with a clear message.","Use WriteAt/Write for output handles and a separate Open() call for reading back content."],"exampleFix":"// before\nf, _ := store.Create(ctx, \"out.txt\")\nf.ReadAll(ctx) // error: file not open for reading\n// after\nf, _ := store.Open(ctx, \"out.txt\")\ndata, err := f.ReadAll(ctx)","handlingStrategy":"validation","validationCode":"func canRead(f interface{ Read(context.Context, []byte) (int, error) }, openedForRead bool) error {\n    if !openedForRead {\n        return errors.New(\"handle not opened for reading: reopen via Open() before Read/ReadAll\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"data, err := f.ReadAll(ctx)\nif err != nil && errors.Is(err, datasource.ErrFileNotOpenForReading) {\n    f, err = store.Open(ctx, path) // reopen in read mode\n    if err != nil { return err }\n    data, err = f.ReadAll(ctx)\n}","preventionTips":["Open with read intent (Open) whenever you plan to call Read/ReadAll.","Treat Create/Append handles as write-only in this abstraction.","Track the open mode alongside handles and assert before read calls.","After writes, close and reopen the file to read content back."],"tags":["file","read","open-mode","gofr"],"backgroundTag":"file-not-open-for-reading","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}