error-router

command
v14.0.1 Latest Latest
Warning

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

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

Documentation

Overview

errors.Router: handlers and middleware that return their errors.

errors.NewRouter wraps any Iris router so every handler has the signature func(iris.Context) error. A returned error goes straight through HandleError: the same pipeline and the same central map as errors.HandleError, which removes the repetitive "if err != nil { errors.HandleError(ctx, err); return }" block. Use registers middleware on matched routes; UseRouter registers pre-routing middleware that also runs for unmatched paths under the router's prefix. A nil return continues the chain, a non-nil error stops the request.

An ordinary iris.Handler still belongs on the wrapped router underneath, reachable as r.Router: the guard below is middleware/apikey, which renders its own 401 and has no error to return.

Learn: write handlers and middleware that return error and let errors.NewRouter render them through the central map. Before: ../validation-errors

Run:

go run main.go

Try:

curl -i http://localhost:8080/api/notes/1  -H "X-API-Key: secret"   # 200
curl -i http://localhost:8080/api/notes/42 -H "X-API-Key: secret"   # 404 NOT_FOUND
curl -i http://localhost:8080/api/notes/1                           # 401 UNAUTHENTICATED
curl -i http://localhost:8080/api/unknown  -H "X-API-Key: secret"   # 404, no route
curl -i -X POST http://localhost:8080/api/notes -H "X-API-Key: secret" \
  -d '{"text":"read the errors chapter"}'                           # 201
curl -i -X POST http://localhost:8080/api/notes -H "X-API-Key: secret" \
  -d 'nope'                                                         # 400 INVALID_ARGUMENT
curl -i -X POST http://localhost:8080/api/notes -H "X-API-Key: secret" \
  -d '{"text":""}'                                                  # 400 INVALID_ARGUMENT

Next: ../../09-authentication/basicauth

Jump to

Keyboard shortcuts

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