pub struct Problem {
pub type_uri: Option<String>,
pub title: Option<String>,
pub status: Option<u16>,
pub detail: Option<String>,
pub instance: Option<String>,
pub extensions: Map<String, Value>,
/* private fields */
}Expand description
An RFC 7807 Problem Details object.
Represents a structured error response per
RFC 7807. All standard fields
are optional and omitted from JSON when None. Extension fields are
flattened into the top-level JSON object.
§Internal Cause
Use Problem::with_cause to attach a diagnostic error that is never
serialized to JSON. This is essential for 5xx errors where you want to
log the root cause server-side without exposing it to clients.
§Example
use rust_rfc7807::Problem;
let problem = Problem::bad_request()
.title("Invalid input")
.detail("The 'email' field is required");
let json = serde_json::to_value(&problem).unwrap();
assert_eq!(json["status"], 400);
assert_eq!(json["title"], "Invalid input");Fields§
§type_uri: Option<String>A URI reference that identifies the problem type.
Defaults to "about:blank" per RFC 7807 when absent.
title: Option<String>A short, human-readable summary of the problem type.
status: Option<u16>The HTTP status code for this occurrence of the problem.
detail: Option<String>A human-readable explanation specific to this occurrence.
instance: Option<String>A URI reference that identifies this specific occurrence.
extensions: Map<String, Value>Extension fields beyond the RFC 7807 standard fields.
Implementations§
Source§impl Problem
impl Problem
Sourcepub fn new(status: u16) -> Self
pub fn new(status: u16) -> Self
Create a new problem with the given HTTP status code.
Per RFC 7807 §4.2, when no type is set the problem type defaults
to "about:blank", and the title SHOULD match the HTTP status
phrase. This constructor sets the title automatically.
use rust_rfc7807::Problem;
let p = Problem::new(429);
assert_eq!(p.status, Some(429));
assert_eq!(p.title.as_deref(), Some("Too Many Requests"));Sourcepub fn bad_request() -> Self
pub fn bad_request() -> Self
400 Bad Request.
401 Unauthorized.
Sourcepub fn validation() -> Self
pub fn validation() -> Self
422 Unprocessable Entity with validation defaults.
Sets status to 422, type to "validation_error", and title to
"Validation failed". Add field errors with push_error
and push_error_code.
use rust_rfc7807::Problem;
let p = Problem::validation()
.push_error("email", "is required");
let json = serde_json::to_value(&p).unwrap();
assert_eq!(json["status"], 422);
assert_eq!(json["type"], "validation_error");Sourcepub fn unprocessable_entity() -> Self
pub fn unprocessable_entity() -> Self
422 Unprocessable Entity (without validation defaults).
Sourcepub fn too_many_requests() -> Self
pub fn too_many_requests() -> Self
429 Too Many Requests.
Sourcepub fn internal_server_error() -> Self
pub fn internal_server_error() -> Self
500 Internal Server Error.
Returns a problem with safe generic defaults:
- title:
"Internal Server Error" - detail:
"An unexpected error occurred."
Use with_cause to attach a diagnostic error for
server-side logging without leaking it to clients.
Source§impl Problem
impl Problem
Sourcepub fn type_(self, type_uri: impl Into<String>) -> Self
pub fn type_(self, type_uri: impl Into<String>) -> Self
Set the problem type URI.
The method is named type_ because type is a Rust keyword.
Sourcepub fn code(self, code: impl Into<String>) -> Self
pub fn code(self, code: impl Into<String>) -> Self
Set the "code" extension field — a stable string code for clients.
Sourcepub fn request_id(self, request_id: impl Into<String>) -> Self
pub fn request_id(self, request_id: impl Into<String>) -> Self
Set the "request_id" extension field.
Sourcepub fn extension(self, key: impl Into<String>, value: impl Into<Value>) -> Self
pub fn extension(self, key: impl Into<String>, value: impl Into<Value>) -> Self
Add an arbitrary extension field.
Sourcepub fn push_error(
self,
field: impl Into<String>,
message: impl Into<String>,
) -> Self
pub fn push_error( self, field: impl Into<String>, message: impl Into<String>, ) -> Self
Append a field-level validation error.
Creates or appends to the "errors" extension array.
Sourcepub fn push_error_code(
self,
field: impl Into<String>,
message: impl Into<String>,
code: impl Into<String>,
) -> Self
pub fn push_error_code( self, field: impl Into<String>, message: impl Into<String>, code: impl Into<String>, ) -> Self
Append a field-level validation error with an error code.
Creates or appends to the "errors" extension array.
Sourcepub fn errors(self, items: Vec<ValidationItem>) -> Self
pub fn errors(self, items: Vec<ValidationItem>) -> Self
Replace the "errors" extension with a complete list of validation items.
Sourcepub fn with_cause(self, err: impl Error + Send + Sync + 'static) -> Self
pub fn with_cause(self, err: impl Error + Send + Sync + 'static) -> Self
Attach an internal cause for server-side diagnostics.
The cause is never serialized to JSON. Access it via
internal_cause for logging.
Sourcepub fn with_cause_str(self, message: impl Into<String>) -> Self
pub fn with_cause_str(self, message: impl Into<String>) -> Self
Attach a string message as the internal cause.
Convenience alternative to with_cause when you
don’t have a typed error.
Source§impl Problem
impl Problem
Sourcepub const ABOUT_BLANK: &'static str = "about:blank"
pub const ABOUT_BLANK: &'static str = "about:blank"
The default problem type URI per RFC 7807 §4.2.
When the "type" member is absent, its value is assumed to be
"about:blank", indicating that the problem has no additional
semantics beyond the HTTP status code.
Sourcepub fn get_type(&self) -> &str
pub fn get_type(&self) -> &str
Returns the effective problem type URI.
Returns the "type" value if set, or ABOUT_BLANK
per RFC 7807 §4.2 when absent.
Sourcepub fn status_code(&self) -> u16
pub fn status_code(&self) -> u16
Returns the HTTP status code, defaulting to 500 if not set.
Sourcepub fn is_server_error(&self) -> bool
pub fn is_server_error(&self) -> bool
Returns true if the status code is 5xx.
Sourcepub fn get_trace_id(&self) -> Option<&str>
pub fn get_trace_id(&self) -> Option<&str>
Returns the "trace_id" extension value, if set.
Sourcepub fn internal_cause(&self) -> Option<&(dyn Error + Send + Sync)>
pub fn internal_cause(&self) -> Option<&(dyn Error + Send + Sync)>
Returns the internal cause message, if set.
This value is never included in serialized output and is intended for server-side logging only.
Sourcepub fn to_json_string_pretty(&self) -> String
pub fn to_json_string_pretty(&self) -> String
Serialize to a pretty-printed JSON string. Useful in tests and debugging.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Problem
impl<'de> Deserialize<'de> for Problem
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Error for Problem
impl Error for Problem
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl IntoProblem for Problem
impl IntoProblem for Problem
Source§fn into_problem(self) -> Problem
fn into_problem(self) -> Problem
Problem instance.