pub trait IntoApiError {
// Required methods
fn context_status(
self,
status: StatusCode,
title: &str,
detail: &str,
) -> ApiError;
fn context_bad_request(self, title: &str, detail: &str) -> ApiError;
fn context_unauthorized(self, title: &str, detail: &str) -> ApiError;
fn context_forbidden(self, title: &str, detail: &str) -> ApiError;
fn context_not_found(self, title: &str, detail: &str) -> ApiError;
fn context_internal(self, title: &str, detail: &str) -> ApiError;
}Expand description
Extension trait for converting any error type into ApiError with HTTP status codes.
This trait is implemented for all types that can be converted into anyhow::Error.
It provides methods to directly convert errors into ApiError instances with
specific HTTP status codes.
§Example
use anyhow::anyhow;
use axum_anyhow::{ApiError, IntoApiError};
let error = anyhow!("Something went wrong");
let api_error: ApiError = error.context_internal("Internal Error", "Database failed");Required Methods§
Sourcefn context_status(
self,
status: StatusCode,
title: &str,
detail: &str,
) -> ApiError
fn context_status( self, status: StatusCode, title: &str, detail: &str, ) -> ApiError
Converts an error to an ApiError with a custom status code.
§Arguments
status- The HTTP status code to usetitle- A short, human-readable summary of the errordetail- A detailed explanation of the error
Sourcefn context_bad_request(self, title: &str, detail: &str) -> ApiError
fn context_bad_request(self, title: &str, detail: &str) -> ApiError
Converts an error to a 400 Bad Request error.
§Arguments
title- A short, human-readable summary of the errordetail- A detailed explanation of the error
Converts an error to a 401 Unauthorized error (missing or invalid credentials).
§Arguments
title- A short, human-readable summary of the errordetail- A detailed explanation of the error
Sourcefn context_forbidden(self, title: &str, detail: &str) -> ApiError
fn context_forbidden(self, title: &str, detail: &str) -> ApiError
Converts an error to a 403 Forbidden error (authenticated but lacks permissions).
§Arguments
title- A short, human-readable summary of the errordetail- A detailed explanation of the error