#[derive(ProblemDetails)]
{
// Attributes available to this derive:
#[status_code]
#[problem_type]
#[title]
#[detail]
#[instance]
}
Expand description
ProblemDetails derive macro
Derives both HttpStatusCode and ToProblemDetails traits for error enums.
This macro automatically generates implementations for converting error variants
to HTTP status codes and RFC 7807 Problem Details responses.
Each variant must have a #[status_code(code)] attribute.
§Supported Attributes
#[status_code(code)]- Required: HTTP status code (e.g., 400, 404, 500)#[problem_type("uri")]- Optional: Custom problem type URI#[title("title")]- Optional: Custom problem title#[detail("detail")]- Optional: Custom problem detail message#[instance("uri")]- Optional: Problem instance URI
§Title Compatibility
The title field can be automatically derived from the #[error("...")] attribute
if no explicit #[title("...")] is provided. This provides compatibility with
thiserror::Error and reduces duplication.
§Basic Example
ⓘ
use spring_web::ProblemDetails;
#[derive(ProblemDetails)]
pub enum ApiError {
#[status_code(400)]
ValidationError,
#[status_code(404)]
NotFound,
#[status_code(500)]
InternalError,
}§Advanced Example with Custom Attributes
ⓘ
#[derive(ProblemDetails)]
pub enum ApiError {
// Explicit title
#[status_code(400)]
#[title("Input Validation Failed")]
#[detail("The provided input data is invalid")]
#[error("Validation error")]
ValidationError,
// Title derived from error attribute
#[status_code(422)]
#[detail("Request data failed validation")]
#[error("Validation Failed")] // This becomes the title
ValidationFailed,
// Full customization
#[status_code(404)]
#[problem_type("https://api.example.com/problems/not-found")]
#[title("Resource Not Found")]
#[detail("The requested resource could not be found")]
#[instance("/users/123")]
#[error("Not found")]
NotFound,
}This will automatically implement:
HttpStatusCodetrait for getting HTTP status codesToProblemDetailstrait for converting to Problem Details responses- OpenAPI integration for documentation generation