Error Handling
zero separates three error spaces. Know which one you're in.
HTTP handler errors (HttpError)
Handlers return !?T (or !void). Returning one of the HttpError tags makes the framework pick the right status instead of always 500:
| Error | HTTP status |
|---|---|
error.InvalidParam, error.MissingParam | 400 |
error.EntityNotFound, error.InvalidRoute | 404 |
error.EntityAlreadyExist | 409 |
error.RequestTimeout | 408 |
error.PanicRecovery | 500 |
anything else (including a raw error.X) | 500 |
statusForError(err) is the single mapping — add a tag there if you need a new 4xx class. Use those tags for client mistakes; server faults should surface as 500 so they're not mistaken for user error.
Outbound service errors (ClientError)
Clients registered with app.addHttpService fail with ClientError:
ServiceNotReachable | CircuitOpen | RateLimited |
OAuthTokenFetchFailed | ResponseParseFailed |
(std.http.Client.FetchError) | (HttpError)- Wrap calls in
tryand return anHttpErrorto your caller, or handle the breaker/retry result explicitly. CircuitOpenmeans the downstream is already known-bad — don't retry; fail fast or degrade.429and5xxare retried withRetry-Afterhonoring before the breaker trips.
See Http Service for service setup and Resilience for breakers and retries.
Datasource errors
ctx.SQL / ctx.NoSQL return backend-specific errors (error.PostgresQueryFailed, error.SQLiteError, …). Treat them as 500 unless you translate them into an HttpError at the handler boundary.
The health probe classifies them via classifyHttpStatus / ErrorKind, so /.well-known/health reflects backend state without leaking messages across threads: lastError() returns the class by value, never a shared buffer.
Logging vs returning
ctx.err(message)logs an error line for diagnostics; it does not send a response. Always return or propagate an error after logging, or the request hangs.- Don't log secrets (tokens, passwords) — the logger redacts nothing.
- Panics are caught by the
panichook (which prints a stack trace) and becomeerror.PanicRecovery→ 500. Fix the unwrap, don't rely on it.
Rule of thumb
Return an HttpError at the handler boundary to tell clients what went wrong; let backend and service errors stay 5xx unless you can map them to a 4xx. Log once, return once.