axum_anyhow/
lib.rs

1#![doc = include_str!("../README.md")]
2
3mod error;
4mod extensions;
5mod helpers;
6mod hook;
7
8pub use error::{ApiError, ApiErrorBuilder};
9pub use extensions::{IntoApiError, OptionExt, ResultExt};
10pub use helpers::{bad_request, forbidden, internal_error, not_found, unauthorized};
11pub use hook::set_error_hook;
12
13use anyhow::Result;
14
15/// A type alias for `Result<T, ApiError>`.
16///
17/// Use this as the return type for Axum handlers to automatically convert errors
18/// into HTTP responses.
19///
20/// # Example
21///
22/// ```rust
23/// use axum::Json;
24/// use axum_anyhow::ApiResult;
25/// use serde::Serialize;
26///
27/// #[derive(Serialize)]
28/// struct Response {
29///     message: String,
30/// }
31///
32/// async fn handler() -> ApiResult<Json<Response>> {
33///     Ok(Json(Response {
34///         message: "Success".to_string(),
35///     }))
36/// }
37/// ```
38pub type ApiResult<T> = Result<T, ApiError>;