Error wrapping

Adding context to an error while keeping the original inside it, with fmt.Errorf("menu %d: %w", id, err). errors.Is and errors.As walk the chain of wrapped errors; %v would flatten it into text and lose the cause.

Minimal example

go
return fmt.Errorf("menu %d: %w", id, ErrNotFound)

if errors.Is(err, ErrNotFound) { /* 404 */ }

Learn this concept

Lesson 2.13 · Error classification in Go

Used in exercises

Exercise 2.14 · POST /v1/menu/price: price a configured item Exercise 3.22 · POST /v1/auth/refresh: detect token reuse Exercise 3.23 · Concurrent outbox relay Exercise 7.16 · Filesystem secret scanner Exercise 7.18 · Release rollback planner
Prerequisite conceptsGo errors