Errors with useful meaning
Return context without leaking internals.
The idea
A store may return a not-found sentinel, a cancellation error, or an unexpected database failure. Preserve the error chain with wrapping so callers can classify behavior. Translate internal errors at the transport boundary and log a controlled event; do not return SQL or connection details to the browser.
Follow the flow
Make it concrete
This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.
if err != nil {
return Booking{}, fmt.Errorf("find booking: %w", err)
}
// At the boundary:
if errors.Is(err, ErrNotFound) {
return fiber.ErrNotFound
}Decisions that matter
- Wrapping preserves a cause while adding operation context.
- Use errors.Is and errors.As rather than comparing error strings.
- Expected domain errors and infrastructure failures need different handling.
Your turn
A database error includes host information. What should the API return and what should its test assert?
Reveal a worked answer
Return a stable generic server error and a correlation identifier. Assert the public status and response contract, and verify the SQL and connection string are absent. Retain operational detail only in an appropriately protected logging channel.