ApiError

Struct ApiError 

Source
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: StatusCode

The HTTP status code for this error

§title: String

A short, human-readable summary of the error

§detail: String

A detailed explanation of the error

§error: Option<Error>

The underlying error that caused this API error

Implementations§

Source§

impl ApiError

Source

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();
Source

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 Debug for ApiError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ApiError

Source§

fn default() -> Self

Creates a default ApiError with:

  • status: StatusCode::INTERNAL_SERVER_ERROR
  • title: "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
where E: Into<Error>,

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§

fn from(err: E) -> Self

Converts to this type from the input type.
Source§

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.

Source§

fn into_response(self) -> Response

Create a response.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,