#error-response #error #http-errors

anyhow-http

Customizable HTTP errors built on anyhow

5 releases (3 breaking)

0.4.0 Sep 2, 2024
0.4.0-rc.1 May 23, 2024
0.3.0 Mar 12, 2024
0.2.0 Jan 3, 2024
0.1.0 Nov 27, 2023

#2 in #http-error

Download history 33/week @ 2024-06-24 53/week @ 2024-07-01 105/week @ 2024-07-08 127/week @ 2024-07-15 101/week @ 2024-07-22 101/week @ 2024-07-29 24/week @ 2024-08-05 45/week @ 2024-08-12 95/week @ 2024-08-19 180/week @ 2024-08-26 388/week @ 2024-09-02 70/week @ 2024-09-09 11/week @ 2024-09-16 96/week @ 2024-09-23 99/week @ 2024-09-30 152/week @ 2024-10-07

360 downloads per month

MIT license

32KB
646 lines

anyhow-http



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

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(())
}

License

Licensed under MIT.

Dependencies

~1.4–2.8MB
~56K SLoC