gofr-dev/gofr · warning

out of range

Error message

out of range

What it means

ErrOutOfRange is a public sentinel error from the datasource interface, analogous to io.EOF/Bounds errors, returned when a ReadAt or Seek operation requests a position outside the file's bounds. ReadAt returns it when the requested offset exceeds file length; ValidateSeekOffset-based helpers return it for invalid whence or offsets beyond length.

Source

Thrown at pkg/gofr/datasource/interface.go:60

	// OpenFile opens a file using the given flags and the given mode.
	OpenFile(name string, flag int, perm os.FileMode) (File, error)

	// Remove removes a file identified by name, returning an error, if any
	// happens.
	Remove(name string) error

	// RemoveAll removes a directory path and any children it contains. It
	// does not fail if the path does not exist (return nil).
	RemoveAll(path string) error

	// Rename renames a file.
	Rename(oldname, newname string) error
}

var (
	ErrFileClosed        = errors.New("File is closed")
	ErrOutOfRange        = errors.New("out of range")
	ErrTooLarge          = errors.New("too large")
	ErrFileNotFound      = os.ErrNotExist
	ErrFileExists        = os.ErrExist
	ErrDestinationExists = os.ErrExist
)

type FileSystemProvider interface {
	FileSystem

	// UseLogger sets the logger for the FileSystem client.
	UseLogger(logger any)

	// UseMetrics sets the metrics for the FileSystem client.
	UseMetrics(metrics any)

	// Connect establishes a connection to FileSystem and registers metrics using the provided configuration when the client was Created.
	Connect()
}

View on GitHub (pinned to 187eb24962)

Solutions

  1. Compare the requested offset against the file size (Stat/Size) before ReadAt/Seek
  2. Treat ErrOutOfRange as end-of-data in read loops (errors.Is check)
  3. Fix whence usage — use io.SeekStart/Current/End constants

Example fix

// before
buf := make([]byte, 10)
f.ReadAt(buf, int64(size)) // out of range
// after
if off >= size {
    return io.EOF // end of data
}
f.ReadAt(buf, off)
Defensive patterns

Strategy: type-guard

Validate before calling

if off < 0 || off >= fileSize {
    return io.EOF
}
f.ReadAt(buf, off)

Type guard

func isOutOfRange(err error) bool {
    return errors.Is(err, datasource.ErrOutOfRange)
}

Try / catch

_, err := f.ReadAt(buf, off)
if errors.Is(err, datasource.ErrOutOfRange) {
    return io.EOF // treat as end of data
}

Prevention

When it happens

Trigger: ReadAt(buf, off) with off >= file size; Seek with an invalid whence or a resulting offset beyond the file length (whence-based beyond length cases).

Common situations: Reading paged binary data where pagination ran past the end; offsets computed from stale file sizes after the file shrank; misused whence constants in Seek calls.

Related errors


AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01). Data as JSON: /api/errors/f46260ef2d3572d0. Report an issue: GitHub.