liveBook · Manning

Error propagation

As explained in Learn Go with Pocket-Sized Projects

Error propagation is the practice of passing an error to an upper program layer when the current layer cannot resolve it.

Error propagation passes an unresolved error to an upper program layer, adding context so that layer can decide how to handle it. Go commonly represents success or failure through a function's final error return value: callers check `if err != nil` immediately. section 5.1.3

Apply the technique at each call boundary:

  • Handle the error locally when the current layer can resolve it. section 5.1.3
  • Otherwise wrap it with `fmt.Errorf` and `%w`, preserving the underlying error while adding context. section 5.1.3

Propagating a validation error

`validateGuess` returns `errInvalidWordLength` wrapped with expected and actual lengths. The `ask` method receives that error and prints its message instead of accepting the guess; tests use `errors.Is` to verify the underlying error.

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains immediate error checks, contextual wrapping, caller behavior, and unit verification, stopping short of broader error-taxonomy design.
Examples
Demonstrates a word-length validator, a wrapped `fmt.Errorf` return, terminal error reporting, and `errors.Is` tests.
What you can do after
Build error-aware Go call chains that wrap unresolved failures, preserve their identity, and let callers respond appropriately.
How it connects
Connects error handling with error wrapping, input validation, caller responsibility, and unit testing.

Also explained in

Compare how 4 books explain error propagation →

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.