Skip to main content

ProblemJson

Struct ProblemJson 

Source
pub struct ProblemJson {
    pub type: String,
    pub title: String,
    pub status: u16,
    pub detail: String,
    pub instance: Option<String>,
    pub extensions: BTreeMap<String, Value>,
}
Expand description

RFC 7807 / 9457 Problem Details response body with optional extension members.

Unlike ApiError (which carries in-process state such as source and Arc), ProblemJson is a pure serialization type — every field maps directly to the wire format.

The extensions map serializes flat into the JSON object, so arbitrary key-value members (e.g. trace_id, request_id) appear at the top level alongside the standard fields:

{
  "type":     "urn:api-bones:error:resource-not-found",
  "title":    "Resource Not Found",
  "status":   404,
  "detail":   "Booking 42 not found",
  "instance": "urn:uuid:01234567-89ab-cdef-0123-456789abcdef",
  "trace_id": "abc123"
}

Content-Type is application/problem+json.

§no_std support

Available when either std or alloc is enabled together with serde (required for serde_json::Value and BTreeMap). Uses BTreeMap internally so heap allocation is the only requirement — std is not needed.

§Examples

use api_bones::error::{ApiError, ErrorCode, ProblemJson};

let err = ApiError::not_found("booking 42 not found");
let problem = ProblemJson::from(err);
assert_eq!(problem.status, 404);
assert_eq!(problem.title, "Resource Not Found");

Adding extension members:

use api_bones::error::{ApiError, ProblemJson};

let mut problem = ProblemJson::from(ApiError::internal("db timeout"));
problem.extensions.insert("trace_id".into(), "abc123".into());
assert_eq!(problem.extensions["trace_id"], "abc123");

Fields§

§type: String

Machine-readable error type URI (RFC 9457 §3.1.1 type).

§title: String

Human-friendly summary (RFC 9457 §3.1.2 title).

§status: u16

HTTP status code (RFC 9457 §3.1.3 status).

§detail: String

Human-readable specifics (RFC 9457 §3.1.4 detail).

§instance: Option<String>

URI identifying this occurrence (RFC 9457 §3.1.5 instance).

§extensions: BTreeMap<String, Value>

Flat extension members (e.g. trace_id, request_id).

Serialized inline at the top level of the JSON object via #[serde(flatten)]. Keys must not collide with the standard fields. Uses BTreeMap for no_std compatibility.

Implementations§

Source§

impl ProblemJson

Source

pub fn new( type: impl Into<String>, title: impl Into<String>, status: u16, detail: impl Into<String>, ) -> Self

Build a ProblemJson directly from its components.

§Examples
use api_bones::error::ProblemJson;

let p = ProblemJson::new(
    "urn:api-bones:error:bad-request",
    "Bad Request",
    400,
    "missing field `email`",
);
assert_eq!(p.status, 400);
assert!(p.extensions.is_empty());
Source

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

Set the instance field (RFC 9457 §3.1.5).

§Examples
use api_bones::error::ProblemJson;

let p = ProblemJson::new("urn:api-bones:error:bad-request", "Bad Request", 400, "oops")
    .with_instance("urn:uuid:00000000-0000-0000-0000-000000000000");
assert!(p.instance.is_some());
Source

pub fn extend(&mut self, key: impl Into<String>, value: impl Into<Value>)

Insert an extension member.

§Examples
use api_bones::error::ProblemJson;

let mut p = ProblemJson::new("urn:api-bones:error:bad-request", "Bad Request", 400, "oops");
p.extend("trace_id", "abc123");
assert_eq!(p.extensions["trace_id"], "abc123");

Trait Implementations§

Source§

impl Clone for ProblemJson

Source§

fn clone(&self) -> ProblemJson

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 ProblemJson

Source§

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

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

impl<'de> Deserialize<'de> for ProblemJson

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<ApiError> for ProblemJson

Available on crate features std and serde only.
Source§

fn from(err: ApiError) -> Self

Convert an ApiError into a ProblemJson.

  • codetype via ErrorCode::urn
  • request_id (UUID) → instance as "urn:uuid:<id>"
  • errors (validation) → "errors" extension member
  • source is dropped (not part of the wire format)
§Examples
use api_bones::error::{ApiError, ErrorCode, ProblemJson};

let err = ApiError::new(ErrorCode::Forbidden, "not allowed");
let p = ProblemJson::from(err);
assert_eq!(p.status, 403);
assert_eq!(p.title, "Forbidden");
Source§

impl PartialEq for ProblemJson

Source§

fn eq(&self, other: &ProblemJson) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for ProblemJson

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 StructuralPartialEq for ProblemJson

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,