error wrapping

Adding context to an error while keeping the original error inside it, so the message explains what failed and code can still test the cause.

In more detail

Each layer adds the operation it was doing, giving messages like read config: missing. The wrapped errors form a chain that errors.Is and errors.As search. Wrapping makes the inner error part of your function's promise to callers, so wrap deliberately.

Example

return fmt.Errorf("read config: %w", ErrMissing)

Related terms

Taught in

References