pub struct ProblemDetails {
pub type_uri: String,
pub title: String,
pub status: u16,
pub detail: Option<String>,
pub instance: Option<String>,
pub extensions: HashMap<String, Value>,
}Expand description
RFC 9457 Problem Details for HTTP APIs
A machine-readable format for specifying errors in HTTP API responses.
Per RFC 9457, all fields are optional. The type field defaults to “about:blank”
if not specified.
§Content-Type
Responses using this struct should set:
Content-Type: application/problem+json§Example
{
"type": "https://spikard.dev/errors/validation-error",
"title": "Request Validation Failed",
"status": 422,
"detail": "2 validation errors in request body",
"errors": [...]
}Fields§
§type_uri: StringA URI reference that identifies the problem type. Defaults to “about:blank” when absent. Should be a stable, human-readable identifier for the problem type.
title: StringA short, human-readable summary of the problem type. Should not change from occurrence to occurrence of the problem.
status: u16The HTTP status code generated by the origin server. This is advisory; the actual HTTP status code takes precedence.
detail: Option<String>A human-readable explanation specific to this occurrence of the problem.
instance: Option<String>A URI reference that identifies the specific occurrence of the problem. It may or may not yield further information if dereferenced.
extensions: HashMap<String, Value>Extension members - problem-type-specific data. For validation errors, this typically contains an “errors” array.
Implementations§
Source§impl ProblemDetails
impl ProblemDetails
Sourcepub const TYPE_VALIDATION_ERROR: &'static str = "https://spikard.dev/errors/validation-error"
pub const TYPE_VALIDATION_ERROR: &'static str = "https://spikard.dev/errors/validation-error"
Standard type URI for validation errors (422)
Sourcepub const TYPE_NOT_FOUND: &'static str = "https://spikard.dev/errors/not-found"
pub const TYPE_NOT_FOUND: &'static str = "https://spikard.dev/errors/not-found"
Standard type URI for not found errors (404)
Sourcepub const TYPE_METHOD_NOT_ALLOWED: &'static str = "https://spikard.dev/errors/method-not-allowed"
pub const TYPE_METHOD_NOT_ALLOWED: &'static str = "https://spikard.dev/errors/method-not-allowed"
Standard type URI for method not allowed (405)
Sourcepub const TYPE_INTERNAL_SERVER_ERROR: &'static str = "https://spikard.dev/errors/internal-server-error"
pub const TYPE_INTERNAL_SERVER_ERROR: &'static str = "https://spikard.dev/errors/internal-server-error"
Standard type URI for internal server error (500)
Sourcepub const TYPE_BAD_REQUEST: &'static str = "https://spikard.dev/errors/bad-request"
pub const TYPE_BAD_REQUEST: &'static str = "https://spikard.dev/errors/bad-request"
Standard type URI for bad request (400)
Sourcepub fn new(
type_uri: impl Into<String>,
title: impl Into<String>,
status: StatusCode,
) -> Self
pub fn new( type_uri: impl Into<String>, title: impl Into<String>, status: StatusCode, ) -> Self
Create a new ProblemDetails with required fields
Sourcepub fn with_detail(self, detail: impl Into<String>) -> Self
pub fn with_detail(self, detail: impl Into<String>) -> Self
Set the detail field
Sourcepub fn with_instance(self, instance: impl Into<String>) -> Self
pub fn with_instance(self, instance: impl Into<String>) -> Self
Set the instance field
Sourcepub fn with_extension(self, key: impl Into<String>, value: Value) -> Self
pub fn with_extension(self, key: impl Into<String>, value: Value) -> Self
Add an extension field
Sourcepub fn with_extensions(self, extensions: Value) -> Self
pub fn with_extensions(self, extensions: Value) -> Self
Add all extensions from a JSON object
Sourcepub fn from_validation_error(error: &ValidationError) -> Self
pub fn from_validation_error(error: &ValidationError) -> Self
Create a validation error Problem Details from ValidationError
This converts the FastAPI-style validation errors to RFC 9457 format:
type: https://spikard.dev/errors/validation-errortitle: “Request Validation Failed”status: 422detail: Summary of error counterrors: Array of validation error details (as extension field)
Sourcepub fn method_not_allowed(detail: impl Into<String>) -> Self
pub fn method_not_allowed(detail: impl Into<String>) -> Self
Create a method not allowed error
Sourcepub fn internal_server_error(detail: impl Into<String>) -> Self
pub fn internal_server_error(detail: impl Into<String>) -> Self
Create an internal server error
Sourcepub fn internal_server_error_debug(
detail: impl Into<String>,
exception: impl Into<String>,
traceback: impl Into<String>,
request_data: Value,
) -> Self
pub fn internal_server_error_debug( detail: impl Into<String>, exception: impl Into<String>, traceback: impl Into<String>, request_data: Value, ) -> Self
Create an internal server error with debug information
Includes exception details, traceback, and request data for debugging. Only use in development/debug mode.
Sourcepub fn bad_request(detail: impl Into<String>) -> Self
pub fn bad_request(detail: impl Into<String>) -> Self
Create a bad request error
Sourcepub fn status_code(&self) -> StatusCode
pub fn status_code(&self) -> StatusCode
Get the HTTP status code
Trait Implementations§
Source§impl Clone for ProblemDetails
impl Clone for ProblemDetails
Source§fn clone(&self) -> ProblemDetails
fn clone(&self) -> ProblemDetails
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more