CURRICULUM PREVIEWRevision 1 · Open for reviewReview the full course
CONNECTING THE PIECES8 min read

Errors with useful meaning

Return context without leaking internals.

Before you begingo-interfaces

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.

Focused example
Go
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.

Keep exploring

One more piece connected.

Mark this lesson complete when you can explain the idea.