ProblemJson

Struct ProblemJson 

Source
pub struct ProblemJson {
    pub type_uri: Option<Cow<'static, str>>,
    pub title: Cow<'static, str>,
    pub status: u16,
    pub detail: Option<Cow<'static, str>>,
    pub details: Option<String>,
    pub code: AppCode,
    pub grpc: Option<GrpcCode>,
    pub metadata: Option<ProblemMetadata>,
    pub retry_after: Option<u64>,
    pub www_authenticate: Option<String>,
}
Expand description

RFC7807 application/problem+json payload enriched with machine-readable metadata.

Instances are produced by ProblemJson::from_app_error or ProblemJson::from_ref. They power the HTTP adapters and expose transport-neutral data for tests.

§Examples

use masterror::{AppError, ProblemJson};

let problem = ProblemJson::from_ref(&AppError::not_found("missing"));
assert_eq!(problem.status, 404);
assert_eq!(problem.code.as_str(), "NOT_FOUND");

Fields§

§type_uri: Option<Cow<'static, str>>

Canonical type URI describing the problem class.

§title: Cow<'static, str>

Short, human-friendly title describing the error category.

§status: u16

HTTP status code returned to the client.

§detail: Option<Cow<'static, str>>

Optional human-readable detail (redacted when marked private).

§details: Option<String>
Available on non-crate feature serde_json only.

Optional textual details emitted to clients when JSON is disabled.

§code: AppCode

Stable machine-readable code.

§grpc: Option<GrpcCode>

Optional gRPC mapping for multi-protocol clients.

§metadata: Option<ProblemMetadata>

Structured metadata derived from Metadata.

§retry_after: Option<u64>

Retry advice propagated as the Retry-After header.

§www_authenticate: Option<String>

Authentication challenge propagated as WWW-Authenticate.

Implementations§

Source§

impl ProblemJson

Source

pub fn from_app_error(error: AppError) -> Self

Build a problem payload from an owned AppError.

§Preconditions
  • error.code must be a public AppCode (guaranteed by construction).
§Examples
use masterror::{AppCode, AppError, ProblemJson};

let problem = ProblemJson::from_app_error(AppError::conflict("exists"));
assert_eq!(problem.code, AppCode::Conflict);
assert_eq!(problem.status, 409);
Source

pub fn from_ref(error: &AppError) -> Self

Build a problem payload from a borrowed AppError.

This is useful inside middleware that logs while forwarding the error downstream without consuming it.

§Examples
use masterror::{AppError, ProblemJson};

let err = AppError::bad_request("invalid");
let problem = ProblemJson::from_ref(&err);
assert_eq!(problem.status, 400);
assert!(problem.detail.is_some());
Source

pub fn from_error_response(response: ErrorResponse) -> Self

Build a problem payload from a plain ErrorResponse.

Metadata and redaction hints are not available in this conversion.

§Examples
use masterror::{AppCode, ErrorResponse, ProblemJson};

let legacy = ErrorResponse::new(404, AppCode::NotFound, "missing").expect("status");
let problem = ProblemJson::from_error_response(legacy);
assert_eq!(problem.status, 404);
assert_eq!(problem.code.as_str(), "NOT_FOUND");
Source

pub fn status_code(&self) -> StatusCode

Convert numeric status into StatusCode.

Falls back to 500 Internal Server Error if the value is invalid.

§Examples
use http::StatusCode;
use masterror::{AppError, ProblemJson};

let problem = ProblemJson::from_app_error(AppError::service("oops"));
assert_eq!(problem.status_code(), StatusCode::INTERNAL_SERVER_ERROR);
Source

pub fn internal(&self) -> ProblemJsonFormatter<'_>

Formatter exposing internals for diagnostic logging.

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

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> Same for T

Source§

type Output = T

Should always be Self
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.