anyhow_http/lib.rs
1#![cfg_attr(docsrs, feature(doc_cfg))]
2
3//! `anyhow-http` offers customizable HTTP errors built on [`anyhow`] errors.
4//!
5//! This crates acts as a superset of [`anyhow`], extending the functionality to define custom
6//! HTTP error responses.
7//! # Example with `axum`
8//!
9//! ```rust,no_run
10//! use axum::{
11//!    routing::get,
12//!    response::IntoResponse,
13//!    Router,
14//! };
15//! use anyhow_http::{http_error_bail, response::HttpJsonResult};
16//!
17//! #[tokio::main]
18//! async fn main() {
19//!     let app = Router::new()
20//!         .route("/", get(handler));
21//!
22//!     let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
23//!         .await
24//!         .unwrap();
25//!     axum::serve(listener, app).await.unwrap();
26//! }
27//!
28//! fn fallible_operation() -> anyhow::Result<()> {
29//!     http_error_bail!(INTERNAL_SERVER_ERROR, "this is an error")
30//! }
31//!
32//! async fn handler() -> HttpJsonResult<impl IntoResponse> {
33//!     fallible_operation()?;
34//!     Ok(())
35//! }
36//! ```
37
38mod extension;
39mod http_error;
40
41pub use extension::*;
42pub use http_error::*;
43
44#[doc(hidden)]
45pub mod macros;
46
47pub mod response;
48
49pub use http;
50
51#[cfg(feature = "derive")]
52#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
53pub mod derive {
54    pub use anyhow_http_derive::FromHttpError;
55}