Error Strategy
Layer errors: precise domain enums inside, transport mapping at edges, context aggregation in binaries.
Busca en todas las páginas de la documentación
Layer errors: precise domain enums inside, transport mapping at edges, context aggregation in binaries.
// domain
pub enum OrderError { NotFound(OrderId), InvalidState }
// api
impl IntoResponse for ApiError {
fn into_response(self) -> Response { /* status + body */ }
}When to reach for this: More than one crate or external API surface.
User request -> ApiError -> OrderError -> sqlx::Error
Result<T, OrderError>sqlx::Error to OrderError::DatabaseOrderError to 404/409 with problem+jsonmain uses anyhow only for startup banner errorsWhat this demonstrates:
#[from] for infra translation onceWorkspace error crate optional for shared AppError wrapper. Use #[non_exhaustive] on public enums.
#[non_exhaustive] handled.| Alternative | Use When | Don't Use When |
|---|---|---|
| snafu | error crates with context | team standard thiserror |
| StatusCode only | internal tools | Public API |
Mark OrderError::Transient for middleware retry.
tonic::Status from domain variant table.
Export stable error code enum mirroring server.
4xx warn, 5xx error, with trace id.
Table tests variant -> status.
Separate DeserializationError at boundary before domain.
Shared ApiError in api crate re-exported.
core::error::Error trait manual impls.
tracing::error!(error=?e) on 5xx path.
Add variants without breaking with non_exhaustive.
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 16 jul 2026