Skip to main content

ApiError

Derive Macro ApiError 

Source
#[derive(ApiError)]
{
    // Attributes available to this derive:
    #[api]
    #[api_error]
    #[api_default]
}
Expand description

Derive [axum::response::IntoResponse] and [utoipa::IntoResponses] for an error enum from a single per-variant declaration.

Each variant is annotated with #[api_error(status = N, code = "string")] where:

  • status — the HTTP status code as a u16 literal
  • code — an application-level error code string written into the code field of the doxa::ApiErrorBody response body emitted by the generated IntoResponse impl

Multiple variants may share the same status code. The derive groups them at expand time so the OpenAPI spec emits one Response per status with each variant contributing a named example.

§Example

use doxa::{ApiError, ToSchema};
use serde::Serialize;

#[derive(Debug, thiserror::Error, Serialize, ToSchema, ApiError)]
pub enum MyError {
    #[error("validation failed: {0}")]
    #[api(status = 400, code = "validation_error")]
    Validation(String),

    #[error("query failed: {0}")]
    #[api(status = 400, code = "query_error")]
    Query(String),

    #[error("not found: {0}")]
    #[api(status = 404, code = "not_found")]
    NotFound(String),

    #[error("internal error")]
    #[api(status = 500, code = "internal")]
    Internal,
}

The generated IntoResponse impl maps each variant to its declared status and emits an ApiErrorBody envelope with the variant’s code and the variant’s Display output as the message. The IntoResponses impl groups Validation and Query under one 400 response with two examples.