liveBook · Manning

Sentinel error

As explained in Learn Go with Pocket-Sized Projects

A sentinel error is a predefined error value or type that callers can compare or check to identify a specific domain condition.

A sentinel error is a predefined, recognizable error value that callers check to identify a specific domain condition without depending on implementation details. section 5.3.2 section 6.5.3

Implement a sentinel error in Go by:

Empty corpus

Define `type corpusError string` with an `Error() string` method, then declare `const ErrCorpusIsEmpty = corpusError("corpus is empty")`. `ReadCorpus` returns this value when the file contains no data. A table-driven test expects `ErrCorpusIsEmpty` for an empty corpus while expecting 34 words from the English corpus. section 5.3.2

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines the custom type, return sites, wrapping, and tests, but stops short of broader error-taxonomy design.
Examples
Uses empty-corpus detection, ReadCorpus, ErrCorpusIsEmpty, ErrCallingServer, and table-driven tests.
What it assumes
Assumes familiarity with Go functions and errors while teaching custom error types, error returns, wrapping, and checks.
What you can do after
Build it from scratch and apply it via errors.Is to expose stable, package-specific failure conditions.
How it connects
Connects error values with the error interface, error wrapping, errors.Is, and package API design.

Also explained in

Compare how 3 books explain sentinel error →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.