IntoApiError

Trait IntoApiError 

Source
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§

Source

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 use
  • title - A short, human-readable summary of the error
  • detail - A detailed explanation of the error
Source

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 error
  • detail - A detailed explanation of the error
Source

fn context_unauthorized(self, title: &str, detail: &str) -> ApiError

Converts an error to a 401 Unauthorized error (missing or invalid credentials).

§Arguments
  • title - A short, human-readable summary of the error
  • detail - A detailed explanation of the error
Source

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 error
  • detail - A detailed explanation of the error
Source

fn context_not_found(self, title: &str, detail: &str) -> ApiError

Converts an error to a 404 Not Found error.

§Arguments
  • title - A short, human-readable summary of the error
  • detail - A detailed explanation of the error
Source

fn context_internal(self, title: &str, detail: &str) -> ApiError

Converts an error to a 500 Internal Server Error.

§Arguments
  • title - A short, human-readable summary of the error
  • detail - A detailed explanation of the error

Implementors§

Source§

impl<E> IntoApiError for E
where E: Into<Error>,