Crate anyhow_http
source ·Expand description
anyhow-http offers customizable HTTP errors built on anyhow errors.
This crates acts as a superset of anyhow, extending the functionality to define custom
HTTP error responses.
Example with axum
use axum::{
routing::get,
response::IntoResponse,
Router,
};
use anyhow_http::{http_error_ret, response::Result};
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(handler));
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000));
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
fn fallible_operation() -> Result<()> {
http_error_ret!(INTERNAL_SERVER_ERROR, "this is an error")
}
async fn handler() -> Result<impl IntoResponse> {
fallible_operation()?;
Ok(())
}Re-exports
pub use http;
Modules
- Creating responses from
HttpError.
Macros
- Construct an ad-hoc
HttpErrorfrom a status code, optional source error and formatted reason. - Shorthand macro to map to a
HttpErrorfrom any error within.map_err(). - Shorthand macro to return early with a
HttpError.
Structs
HttpErroris an error that can be represented as a HTTP response.HttpErroris generic over its response format, allowing consumers to implement their custom error response. SeeIntoHttpErrorResponse.