Axum-dyn-error
Dynamic error handling for Axum HTTP handlers
[]
= "0.1"
Only supports axum v0.6
This crate provides foundational logic for creating and handling dynamic HTTP errors.
Implementing the HttpError trait on errors allows you to customize how the errors and
displayed in error responses.
I recommend using thiserror for defining your user facing error types.
In order to use the dynamic error handling you should replace your [Result] return types with
the [HttpResult] type from axum_dyn_error.
use ;
use Error;
use ;
/// Dummy structure representing a user
;
/// Mock function for finding a user by id
pub async
/// Example handler
pub async
Anyhow support
Axum-dyn-error supports anyhow errors through the anyhow feature flag, by default the
hide-anyhow feature flag is enabled which prevents the anyhow error message from being
included in the error response instead responding with "Server error".
use ;
use ;
use anyhow;
/// Dummy structure representing a user
;
/// Mock function for finding a user by id
pub async
/// Example handler
pub async
Using [AnyhowStatusExt] the anyhow error types can have an HTTP status code associated with them, by default anyhow errors just use "500 Internal server error":
use ;
use ;
use anyhow;
/// Dummy structure representing a user
;
/// Mock function for finding a user by id
pub async
/// Example handler
pub async
Custom response
By default the responses generated from the errors use the "reason" as a text response
body. You can change this by create a structure and implementing IntoHttpErrorResponse
on that structure:
use ;
use ;
;
// You can then alias the HttpResult type
pub type MyHttpResult<T> = ;
Crate Features
The default features are ["log", "hide-anyhow"]
| Feature | Description |
|---|---|
| log | Logs errors that are created using log::error! |
| anyhow | Adds support for handling anyhow error types |
| hide-anyhow | Replaces anyhow error messages in HTTP responses with a generic server error message |