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: u16HTTP status code returned to the client.
detail: Option<Cow<'static, str>>Optional human-readable detail (redacted when marked private).
details: Option<String>serde_json only.Optional textual details emitted to clients when JSON is disabled.
code: AppCodeStable 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
impl ProblemJson
Sourcepub fn from_app_error(error: AppError) -> Self
pub fn from_app_error(error: AppError) -> Self
Build a problem payload from an owned AppError.
§Preconditions
error.codemust be a publicAppCode(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);Sourcepub fn from_ref(error: &AppError) -> Self
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());Sourcepub fn from_error_response(response: ErrorResponse) -> Self
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");Sourcepub fn status_code(&self) -> StatusCode
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);Trait Implementations§
Source§impl Clone for ProblemJson
impl Clone for ProblemJson
Source§fn clone(&self) -> ProblemJson
fn clone(&self) -> ProblemJson
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more