central-error-map

command
v14.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

The central error map: domain sentinels classified in one place.

The service layer returns plain sentinel errors that know nothing about HTTP. A single errors.NewOptions() value maps each sentinel to a canonical error code and app.Use(opts.Handler()) attaches that map to every request, so handlers only forward errors with ctx.OK or errors.HandleError. Matching goes through errors.Is, so wrapping a sentinel with fmt.Errorf("...: %w") anywhere in the call chain keeps the classification. An error the map does not know about renders as a generic 500 and is logged server-side.

Learn: map domain sentinel errors to HTTP status codes once and forward them from handlers with ctx.OK, ctx.NoContent or errors.HandleError.

Run:

go run main.go

Try:

curl -i http://localhost:8080/books/1                  # 200
curl -i http://localhost:8080/books/42                 # 404 NOT_FOUND
curl -i -X POST http://localhost:8080/books/2/publish  # 403 PERMISSION_DENIED
curl -i -X POST http://localhost:8080/books/3/publish  # 204, no body
curl -i -X POST http://localhost:8080/books/1/publish  # 409 ALREADY_EXISTS
curl -i http://localhost:8080/books/1/stats            # 500 INTERNAL, generic body

Next: ../handle-errorf

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL