Skip to main content

ProblemDetails

Struct ProblemDetails 

Source
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: String

A 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: String

A short, human-readable summary of the problem type. Should not change from occurrence to occurrence of the problem.

§status: u16

The 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

Source

pub const TYPE_VALIDATION_ERROR: &'static str = "https://spikard.dev/errors/validation-error"

Standard type URI for validation errors (422)

Source

pub const TYPE_NOT_FOUND: &'static str = "https://spikard.dev/errors/not-found"

Standard type URI for not found errors (404)

Source

pub const TYPE_METHOD_NOT_ALLOWED: &'static str = "https://spikard.dev/errors/method-not-allowed"

Standard type URI for method not allowed (405)

Source

pub const TYPE_INTERNAL_SERVER_ERROR: &'static str = "https://spikard.dev/errors/internal-server-error"

Standard type URI for internal server error (500)

Source

pub const TYPE_BAD_REQUEST: &'static str = "https://spikard.dev/errors/bad-request"

Standard type URI for bad request (400)

Source

pub fn new( type_uri: impl Into<String>, title: impl Into<String>, status: StatusCode, ) -> Self

Create a new ProblemDetails with required fields

Source

pub fn with_detail(self, detail: impl Into<String>) -> Self

Set the detail field

Source

pub fn with_instance(self, instance: impl Into<String>) -> Self

Set the instance field

Source

pub fn with_extension(self, key: impl Into<String>, value: Value) -> Self

Add an extension field

Source

pub fn with_extensions(self, extensions: Value) -> Self

Add all extensions from a JSON object

Source

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:

Source

pub fn not_found(detail: impl Into<String>) -> Self

Create a not found error

Source

pub fn method_not_allowed(detail: impl Into<String>) -> Self

Create a method not allowed error

Source

pub fn internal_server_error(detail: impl Into<String>) -> Self

Create an internal server error

Source

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.

Source

pub fn bad_request(detail: impl Into<String>) -> Self

Create a bad request error

Source

pub fn status_code(&self) -> StatusCode

Get the HTTP status code

Source

pub fn to_json(&self) -> Result<String, Error>

Serialize to JSON string

§Errors

Returns an error if the serialization fails.

Source

pub fn to_json_pretty(&self) -> Result<String, Error>

Serialize to pretty JSON string

§Errors

Returns an error if the serialization fails.

Trait Implementations§

Source§

impl Clone for ProblemDetails

Source§

fn clone(&self) -> ProblemDetails

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ProblemDetails

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for ProblemDetails

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.