pub struct ApiError {
pub status: StatusCode,
pub title: String,
pub detail: String,
pub error: Option<Error>,
}Expand description
An API error that can be converted into an HTTP response.
This struct contains the HTTP status code, a title, and a detailed description of the error. When converted into a response, it produces a JSON body with these fields.
§JSON Response Format
{
"status": 404,
"title": "Not Found",
"detail": "The requested resource does not exist"
}§Example
use axum::http::StatusCode;
use axum_anyhow::ApiError;
let error = ApiError {
status: StatusCode::NOT_FOUND,
title: "Not Found".to_string(),
detail: "User not found".to_string(),
error: None,
};Fields§
§status: StatusCodeThe HTTP status code for this error
title: StringA short, human-readable summary of the error
detail: StringA detailed explanation of the error
error: Option<Error>The underlying error that caused this API error
Implementations§
Source§impl ApiError
impl ApiError
Sourcepub fn builder() -> ApiErrorBuilder
pub fn builder() -> ApiErrorBuilder
Creates a new builder for constructing an ApiError.
§Example
use axum::http::StatusCode;
use axum_anyhow::ApiError;
use anyhow::anyhow;
let error = ApiError::builder()
.status(StatusCode::BAD_REQUEST)
.title("Validation Error")
.detail("Email address is required")
.build();Sourcepub fn into_error(self) -> Error
pub fn into_error(self) -> Error
Converts this ApiError into an anyhow::Error.
If the ApiError contains an underlying error, it will be returned with
additional context from the title and detail. Otherwise, a new error is
created from the title and detail.
§Example
use axum::http::StatusCode;
use axum_anyhow::ApiError;
use anyhow::anyhow;
let api_error = ApiError::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.title("Database Error")
.detail("Failed to connect")
.error(anyhow!("Connection timeout"))
.build();
let anyhow_error = api_error.into_error();Trait Implementations§
Source§impl Default for ApiError
impl Default for ApiError
Source§fn default() -> Self
fn default() -> Self
Creates a default ApiError with:
status:StatusCode::INTERNAL_SERVER_ERRORtitle:"Internal Error"detail:"Something went wrong"error:None
§Example
use axum::http::StatusCode;
use axum_anyhow::ApiError;
let error = ApiError::default();
assert_eq!(error.status, StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(error.title, "Internal Error");
assert_eq!(error.detail, "Something went wrong");
assert!(error.error.is_none());Source§impl<E> From<E> for ApiError
Converts from anyhow::Error to ApiError.
impl<E> From<E> for ApiError
Converts from anyhow::Error to ApiError.
By default, all errors are converted to 500 Internal Server Error responses. Use the extension traits to specify different status codes.
Source§impl IntoResponse for ApiError
Converts from ApiError to an HTTP Response.
impl IntoResponse for ApiError
Converts from ApiError to an HTTP Response.
This implementation allows ApiError to be used as a return type in Axum handlers.
The error is serialized as JSON with the status code, title, and detail fields.