User Tools

Site Tools


go:errors:sentinel_errors

Sentinel errors

What is it?

A sentinel error is a package-level variable used as a fixed, recognizable error value.

Example:

var ErrNotFound = errors.New("not found")

What is it used for?

  • Let callers check for a specific error using equality (`err == ErrNotFound`)

or (preferably) `errors.Is(err, ErrNotFound)` when wrapping is involved.

Example

package store
 
import "errors"
 
var ErrNotFound = errors.New("not found")

Notes / pitfalls

  • Sentinel errors are simple but can be too generic for large systems.
  • Prefer typed errors when you need extra context (resource/id).
  • If you wrap errors, use `errors.Is` to match the sentinel.

Hard words (English)

  • sentinel /ˈsentɪnəl/: dấu hiệu nhận diện/cột mốc
  • recognizable /ˈrekəɡnaɪzəbəl/: dễ nhận ra
  • generic /dʒəˈnerɪk/: chung chung
go/errors/sentinel_errors.txt · Last modified: by phong2018