axum-error-sets
Typed, composable HTTP error sets for axum, with OpenAPI generation through aide.
Instead of one large error enum per application, each function lists the exact HTTP status codes it can return, as a tuple in its return type, such as ApiResult<T, (Unauthorized, NotFound<String>)>. The sets are built on type-sets.
Features
- Exact error contracts: each function declares which status codes it can return. Returning any other code is a compile error.
- Every status code: there is a wrapper type for every 4xx and 5xx code, with any
IntoResponsetype as the body:NotFound,NotFound<String>,NotFound<Json<MyError>>. ?just works: status codes convert into any error set that contains them, and small sets grow into larger ones with.into_superset().- Error wrapping helpers:
.with_status::<BadRequest>(),.into_status::<..>(),.change_status::<..>()and.map_status(..)on anyResult. - OpenAPI support: with the
aidefeature, every status code in a handler's set is documented in the generated OpenAPI spec.
Installation
[]
= "0.4"
# For OpenAPI generation with aide:
= { = "0.4", = ["aide"] }
Example
use Json;
use ;
async
The examples directory has runnable examples, including handlers, aide integration, and use with axum-typed-routing.
Pairs well with axum-typed-routing
axum-typed-routing lets you declare a route's path and parameters next to its handler, checked at compile time. Combined with its api_route macro, a handler's error set appears in the OpenAPI documentation with no extra annotations:
async
See examples/5_typed_routing.rs.
License
Licensed under either of MIT or Apache-2.0, at your option.