Skip to main content

Crate axum_problem

Crate axum_problem 

Source
Expand description

RFC 9457 problem details for axum.

§Quick start

use axum::{Router, routing::get, extract::Path};
use axum_problem::{AxumProblem, Problem};
use thiserror::Error;

#[derive(Debug, Error, AxumProblem)]
pub enum ApiError {
    #[error("order {id} not found")]
    #[problem(status = 404)]
    NotFound { id: i64 },

    #[error("unauthorized")]
    #[problem(status = 401)]
    Unauthorized,

    #[error("database error: {0}")]
    #[problem(status = 500, mask)]
    Database(String),
}

async fn get_order(Path(id): Path<i64>) -> Result<String, ApiError> {
    Err(ApiError::NotFound { id })
}

The mask attribute hides the error detail from the HTTP response (preventing internal details leaking to clients) and logs the full error via tracing::error!.

Structs§

Problem
An RFC 9457 problem details response.

Functions§

status_title
Returns a short title for common HTTP status codes.

Derive Macros§

AxumProblem
Derive IntoResponse for an error enum, mapping each variant to an RFC 9457 problem details response.