Skip to content

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:

ErrorHTTP status
error.InvalidParam, error.MissingParam400
error.EntityNotFound, error.InvalidRoute404
error.EntityAlreadyExist409
error.RequestTimeout408
error.PanicRecovery500
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 try and return an HttpError to your caller, or handle the breaker/retry result explicitly.
  • CircuitOpen means the downstream is already known-bad — don't retry; fail fast or degrade.
  • 429 and 5xx are retried with Retry-After honoring 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 panic hook (which prints a stack trace) and become error.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.