liveBook · Manning

Error wrapping

Error wrapping adds contextual information to an error while preserving access to the underlying error for later propagation or inspection.

How 3 Manning books explain it

100 Go Mistakes and How to Avoid Them

Error wrapping is the practice of attaching contextual information to an error while preserving the underlying error for propagation and inspection.

How it is used
Adds context or marks propagated errors while preserving their sources for callers.
How deep it goes
Contrasts direct returns, custom error types, wrapping and conversion, then shows how callers inspect wrapped errors.
Examples
Uses a permission-denied database query, a transaction HTTP handler and coordinate validation to illustrate wrapping decisions.
What you can do after
Apply it via fmt.Errorf with %w, and inspect wrapped types or values with errors.As or errors.Is.

Read how 100 Go Mistakes and How to Avoid Them explains error wrapping →

Learn Go with Pocket-Sized Projects

A wrapped error is an error that retains an underlying error while adding contextual information for higher-level handling.

How it is used
Propagates errors from validation or other lower layers while preserving context for higher-level handling.
How deep it goes
Defines %w wrapping, demonstrates a validator, and tests identity with errors.Is without exploring deeper error internals.
Examples
Uses invalid word length, expected-versus-actual counts, fmt.Errorf, and table-driven validator tests.
What you can do after
Apply it via fmt.Errorf with %w and inspect the retained cause using errors.Is.

Read how Learn Go with Pocket-Sized Projects explains error wrapping →

Go by Example

Error wrapping is the practice of adding contextual information to an error while preserving the original error for later inspection.

How it is used
Adds actionable context to failures returned by a custom command-line flag parser.
How deep it goes
Demonstrates contextual wrapping with %w and inspection with errors.Is, without descending into error-interface internals.
Examples
Uses invalid flag values, strconv.ErrSyntax, and a complete custom flag parser.
What you can do after
Apply it via fmt.Errorf to add context while allowing callers to identify the underlying parsing error.

Read how Go by Example explains error wrapping →

Also covered in

Related concepts

Questions about error wrapping

What is error wrapping?

Error wrapping adds contextual information to an error while preserving access to the underlying error for later propagation or inspection.

Which Manning books explain error wrapping?

100 Go Mistakes and How to Avoid Them, Learn Go with Pocket-Sized Projects and Go by Example.

What is the difference between error wrapping and error transformation?

Error wrapping adds contextual information to an error while preserving access to the underlying error for later propagation or inspection. Error transformation is the conversion of an error into another error without preserving the original error for unwrapping.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.