gofr-dev/gofr · error
out of range
Error message
out of range
What it means
ErrOutOfRange is the public sentinel for position-related failures in the S3 datasource. It is wrapped by ReadAt for negative offsets and returned by seek-offset validation when the requested position is invalid (bad whence, negative resulting offset, or offset beyond the known object length). Check with errors.Is(err, s3.ErrOutOfRange).
Source
Thrown at pkg/gofr/datasource/file/s3/file_parse.go:22
"bufio"
"bytes"
"encoding/json"
"errors"
"io"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
file "gofr.dev/pkg/gofr/datasource/file"
)
var (
// errNotPointer is returned when Read method is called with a non-pointer argument.
errStringNotPointer = errors.New("input should be a pointer to a string")
ErrOutOfRange = errors.New("out of range")
)
const (
statusErr = "ERROR"
statusSuccess = "SUCCESS"
)
// textReader implements RowReader for reading text files.
type textReader struct {
scanner *bufio.Scanner
logger Logger
}
// jsonReader implements RowReader for reading JSON files.
type jsonReader struct {
decoder *json.Decoder
token json.Token
}View on GitHub (pinned to 187eb24962)
Solutions
- Clamp offsets to [0, size] before Seek/ReadAt; get the size via Stat().
- Use only os.SEEK_SET/SEEK_CUR/SEEK_END constants.
- Distinguish EOF semantics: for ReadAt, offsets >= size return io.EOF, negatives return this error — validate inputs accordingly.
- Match with errors.Is(err, s3.ErrOutOfRange) and surface a 4xx-style caller error rather than retrying.
Example fix
// before
_, err := f.Seek(int64(size+10), io.SeekStart) // out of range
// after
if newOff < 0 || newOff > size { return ErrOutOfRange }
_, err := f.Seek(newOff, io.SeekStart) Defensive patterns
Strategy: validation
Validate before calling
fi, _ := f.Stat()
size := fi.Size()
func safeSeek(f *s3datasource.S3File, off int64) error {
if off < 0 || off > size {
return fmt.Errorf("offset %d out of [0,%d]", off, size)
}
_, err := f.Seek(off, io.SeekStart)
return err
} Type guard
func inRange(off, size int64) bool { return off >= 0 && off <= size } Try / catch
_, err := f.Seek(off, io.SeekStart)
if errors.Is(err, s3datasource.ErrOutOfRange) {
// clamp and retry: off = min(max(off,0), size)
} Prevention
- Stat the object and clamp offsets to [0, size].
- Use only the io.SeekStart/Current/End constants.
- Handle offsets >= size as EOF (io.EOF), not errors.
- Distinguish ErrOutOfRange (caller bug) from I/O errors when logging.
When it happens
Trigger: 1) ReadAt with a negative offset; 2) Seek to a resulting offset < 0; 3) Seek with an invalid whence; 4) Seek/position beyond the object length where validation rejects it. Test references show seek validation paths (validateSeekOffset) return it too.
Common situations: Seeking past end of file expecting os.File semantics (this implementation rejects it); arithmetic underflow producing negative offsets; using a whence constant outside 0/1/2.
Related errors
- %w: negative offset %d
- incorrect file type
- response retrieved is nil
- s3 backend did not honor the requested byte range
- %w: S3 file is empty
AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01).
Data as JSON: /api/errors/4e1cd1ca5b477221.
Report an issue: GitHub.