Skip to main content

Problem

Struct Problem 

Source
pub struct Problem {
    pub problem_type: Option<String>,
    pub title: String,
    pub status: u16,
    pub detail: Option<String>,
    pub instance: Option<String>,
    pub extensions: Map<String, Value>,
}
Expand description

An RFC 9457 problem details response.

Serializes to application/problem+json with the correct HTTP status code. Extension members (RFC 9457 §3.5) are serialized as top-level JSON fields alongside the standard members.

§Manual construction

use axum_problem::Problem;
use serde_json::json;

let p = Problem::new(422)
    .title("Validation Failed")
    .detail("One or more fields are invalid")
    .extension("violations", json!([
        {"field": "email", "message": "must be a valid email"},
        {"field": "age",   "message": "must be at least 18"},
    ]));

Produces:

{
  "title": "Validation Failed",
  "status": 422,
  "detail": "One or more fields are invalid",
  "violations": [...]
}

Fields§

§problem_type: Option<String>

URI reference identifying the problem type. Defaults to "about:blank".

§title: String

Short human-readable summary of the problem type.

§status: u16

HTTP status code.

§detail: Option<String>

Human-readable explanation specific to this occurrence.

§instance: Option<String>

URI identifying the specific occurrence of this problem.

§extensions: Map<String, Value>

RFC 9457 extension members — serialized as top-level JSON fields.

Use Problem::extension to add fields fluently.

Implementations§

Source§

impl Problem

Source

pub fn new(status: u16) -> Self

Create a new problem for the given HTTP status code. The title is set automatically from the status code.

Source

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

Add an RFC 9457 extension member as a top-level JSON field.

use axum_problem::Problem;
use serde_json::json;

let p = Problem::new(422)
    .extension("violations", json!([{"field":"email","message":"invalid"}]))
    .extension("request_id", json!("req-abc-123"));
Source

pub fn problem_type(self, t: impl Into<String>) -> Self

Set the problem type URI (defaults to "about:blank" in the response).

Source

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

Override the title.

Source

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

Add a human-readable explanation for this specific occurrence.

Source

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

Add a URI identifying this specific occurrence.

Source

pub fn bad_request() -> Self

Source

pub fn unauthorized() -> Self

Source

pub fn forbidden() -> Self

Source

pub fn not_found() -> Self

Source

pub fn method_not_allowed() -> Self

Source

pub fn conflict() -> Self

Source

pub fn unprocessable_entity() -> Self

Source

pub fn too_many_requests() -> Self

Source

pub fn internal_server_error() -> Self

Source

pub fn not_implemented() -> Self

Source

pub fn service_unavailable() -> Self

Trait Implementations§

Source§

impl Clone for Problem

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Problem

Source§

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

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

impl IntoResponse for Problem

Source§

fn into_response(self) -> Response

Create a response.
Source§

impl Serialize for Problem

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
Source§

impl<'__s> ToSchema<'__s> for Problem

Source§

fn schema() -> (&'__s str, RefOr<Schema>)

Return a tuple of name and schema or reference to a schema that can be referenced by the name or inlined directly to responses, request bodies or parameters.
Source§

fn aliases() -> Vec<(&'__s str, Schema)>

Optional set of alias schemas for the ToSchema::schema. 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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, S> Handler<IntoResponseHandler, S> for T
where T: IntoResponse + Clone + Send + 'static,

Source§

type Future = Ready<Response<Body>>

The type of future calling this handler returns.
Source§

fn call( self, _req: Request<Body>, _state: S, ) -> <T as Handler<IntoResponseHandler, S>>::Future

Call the handler with the given request.
Source§

fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>
where L: Layer<HandlerService<Self, T, S>> + Clone, <L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,

Apply a tower::Layer to the handler. Read more
Source§

fn with_state(self, state: S) -> HandlerService<Self, T, S>

Convert the handler into a Service by providing the state
Source§

impl<H, T> HandlerWithoutStateExt<T> for H
where H: Handler<T, ()>,

Source§

fn into_service(self) -> HandlerService<H, T, ()>

Convert the handler into a Service and no state.
Source§

fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>

Convert the handler into a MakeService and no state. Read more
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.