Crates.io | anyhow-http |
lib.rs | anyhow-http |
version | 0.4.0 |
source | src |
created_at | 2023-11-27 10:53:58.884219 |
updated_at | 2024-09-02 09:39:15.554825 |
description | Customizable HTTP errors built on anyhow |
homepage | |
repository | https://github.com/heat1q/anyhow-http |
max_upload_size | |
id | 1050331 |
size | 32,111 |
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.
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 listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
axum::serve(listener, app).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(())
}
Licensed under MIT.