Skip to main content

api_error

Attribute Macro api_error 

Source
#[api_error]
Expand description

Declares a stable domain error as an enum.

Each variant carries its own HTTP status, a stable machine-readable code, and an optional human message; a variant may also declare response headers and a typed details payload.

#[api_error]
enum CreateUserError {
    #[status(409)]
    #[code("email_already_exists")]
    #[message("A user with this email already exists.")]
    EmailAlreadyExists,

    #[status(429)]
    #[code("rate_limited")]
    #[header("retry-after", "30")]
    RateLimited(RateLimitDetails),
}

The code is the stable identity, the same way an operation id is: it reaches the response body, the OpenAPI responses, the generated documentation, and MCP typed-error handling, and it participates in compatibility reports. Returning Result<T, ThisError> from a handler is what declares the error responses on the operation.

Faults the framework raises itself — an invalid response header, a serialization failure — are not projected this way. They are redacted to a generic 500 over HTTP and a generic internal error over MCP.