pub struct Problem { /* private fields */ }
Expand description
A RFC 7807 Problem Error.
§Error Cause
This type provides methods to access the inner error cause. Although we store it, we DO NOT send it when serializing the problem, as it would leak implementation details.
§Backtraces
Many implementations of the RFC add automatic backtrace to the problem. This is NOT done by this type and MUST NOT be added manually, as exposing the backtrace to the caller will expose implementation details and CAN be source of vulnerabilities.
§Custom Problem Types
When an HTTP API needs to define a response that indicates an error condition, it might be appropriate to do so by defining a new problem type.
New problem type definitions MUST document:
- a type URI (typically, with the “http” or “https” scheme),
- a title that appropriately describes it (think short), and
- the HTTP status code for it to be used with.
A problem type definition MAY specify additional members on the problem details object. For example, an extension might use typed links RFC 5988 to another resource that can be used by machines to resolve the problem.
Avoid defining custom problem types, preferring to use standardized HTTP status whenever possible. Custom types should only be defined if no HTTP status code can properly encode the occurred problem. As an example:
{
"type": "https://example.com/probs/out-of-credit",
"status": 403,
"title": "You do not have enough credit",
"detail": "Your current balance is 30, but that costs 50",
"balance": 30,
"accounts": ["/account/12345", "/account/67890"]
}
When adding a new problem type, we suggest that the type reference should also be added to the main API gateway page.
§Error Instances
We currently do not track error instances (the instance
field defined
in the RFC). This may change in the future.
Implementations§
Source§impl Problem
Problem
constructors.
impl Problem
Problem
constructors.
Sourcepub fn custom(status: StatusCode, type: Uri) -> Self
pub fn custom(status: StatusCode, type: Uri) -> Self
Create a custom problem from the given type.
See the type’s documentation for more information about custom types.
Sourcepub fn from_status(status: StatusCode) -> Self
pub fn from_status(status: StatusCode) -> Self
Create a new problem for the given status code.
Sourcepub fn with_title(self, title: impl Into<CowStr>) -> Self
pub fn with_title(self, title: impl Into<CowStr>) -> Self
Sets the title for this problem.
OBS: HTTP Status only problems MUST NOT have their title changed.
This method doesn’t protect against this, be sure to follow the spec.
Sourcepub fn with_detail(self, detail: impl Into<CowStr>) -> Self
pub fn with_detail(self, detail: impl Into<CowStr>) -> Self
Sets the detail for this problem.
Sourcepub fn with_cause<E>(self, cause: E) -> Self
pub fn with_cause<E>(self, cause: E) -> Self
Sets the error cause for this problem.
Sourcepub fn with_extension<E, V>(self, extension: E, value: V) -> Self
pub fn with_extension<E, V>(self, extension: E, value: V) -> Self
Add a new extension value for the problem.
The telemetry
extension is reserved for internal use and the cause
extension is reserved for future use.
§Panics
Panics if field == "cause"
or if the serialization of value
fails.
Source§impl Problem
Getters
impl Problem
Getters
Sourcepub const fn type_(&self) -> &Uri
pub const fn type_(&self) -> &Uri
A URI reference (RFC 3986) that identifies the problem type.
The specification encourages that, when dereferenced, it provide
human-readable documentation for the problem type. When this
member is not present, its value is assumed to be about:blank
.
Sourcepub fn title(&self) -> &str
pub fn title(&self) -> &str
A short, human-readable summary of the problem type.
It SHOULD NOT change from occurrence to occurrence of the problem.
Sourcepub const fn status(&self) -> StatusCode
pub const fn status(&self) -> StatusCode
The HTTP status code generated by the origin server for this occurrence of the problem.
Sourcepub fn details(&self) -> &str
pub fn details(&self) -> &str
A human-readable explanation specific to this occurrence of the problem.
Sourcepub const fn extensions(&self) -> &Extensions
pub const fn extensions(&self) -> &Extensions
Extra members of the problem containing additional information about the specific occurrence.
Sourcepub fn extensions_mut(&mut self) -> &mut Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
Extra members of the problem containing additional information about the specific occurrence.
Source§impl Problem
Error handling methods.
impl Problem
Error handling methods.
Sourcepub fn is<E>(&self) -> bool
pub fn is<E>(&self) -> bool
Returns true if E
is the type of the cause of this problem.
Useful to a failed result is caused by a specific error type.
Sourcepub fn downcast<E>(self) -> Result<E, Self>
pub fn downcast<E>(self) -> Result<E, Self>
Attempts to downcast the problem to a concrete type.
§Errors
Returns the original problem if the underlying cause is not of the specified type.
Sourcepub fn downcast_ref<E>(&self) -> Option<&E>
pub fn downcast_ref<E>(&self) -> Option<&E>
Attempt to downcast the problem to a concrete type by reference.
Sourcepub fn isolate<E>(self) -> Result<Self, Self>
pub fn isolate<E>(self) -> Result<Self, Self>
Attempts to isolate a specific cause to the Err
variant.
This is different from a downcast as we don’t lose backtrace/source location information.
This method is useful when the user wants to handle specific errors
with ?
.
§Errors
Returns Err
when self
is an E
.