#[non_exhaustive]pub struct ProblemDetails {
pub problem_type: String,
pub title: String,
pub status: u16,
pub detail: String,
pub instance: String,
pub retry_after: Option<u64>,
pub suggested_fix: Option<SuggestedFix>,
pub code_actions: Vec<CodeAction>,
pub exit_code: Option<u8>,
}Expand description
An RFC 9457 Problem Details envelope for machine consumers.
Serializes under the application/problem+json media type. It carries
the five standard members (type, title, status, detail,
instance), the three agent extensions (retry_after, suggested_fix,
code_actions), and the optional exit_code extension. retry_after
serializes even when None (as JSON null) so an agent never has to
guess whether a class is transient.
Build one with ProblemDetails::new and the with_* methods, or
(preferred, for a crate’s own error enum) with ProblemMeta::into_details.
§Examples
use mif_problem::{Applicability, ProblemDetails, SuggestedFix};
let problem = ProblemDetails::new(
"https://modeled-information-format.github.io/mif-rs/references/errors/invalid-input/v1",
"Invalid input",
400,
"the supplied file was not valid JSON",
"urn:mif-cli:invalid-input",
)
.with_exit_code(2)
.with_suggested_fix(SuggestedFix::new(
"Check the file is well-formed JSON and retry.",
Applicability::MaybeIncorrect,
));
assert_eq!(problem.status, 400);
assert_eq!(problem.retry_after, None);
assert!(problem.to_json().contains("\"type\""));Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.problem_type: StringA URI reference identifying the problem type. Stable and versioned.
title: StringShort, human-readable summary of the problem type. Stable per type.
status: u16Numeric status mapping to a status class (see also exit_code).
detail: StringHuman-readable explanation specific to this occurrence.
instance: StringURI reference identifying this specific occurrence.
retry_after: Option<u64>When the operation may safely be retried (delta-seconds). Explicitly
null for non-transient errors so agents do not have to guess.
suggested_fix: Option<SuggestedFix>A recovery suggestion, tagged with an applicability marker.
code_actions: Vec<CodeAction>Structured edits the agent can apply directly.
exit_code: Option<u8>The process exit code emitted alongside the error, if known.
Implementations§
Source§impl ProblemDetails
impl ProblemDetails
Sourcepub fn new(
problem_type: impl Into<String>,
title: impl Into<String>,
status: u16,
detail: impl Into<String>,
instance: impl Into<String>,
) -> Self
pub fn new( problem_type: impl Into<String>, title: impl Into<String>, status: u16, detail: impl Into<String>, instance: impl Into<String>, ) -> Self
Creates an envelope from the five RFC 9457 standard members.
retry_after, suggested_fix, code_actions, and exit_code start
empty; add them with the with_* methods.
§Arguments
problem_type- Stable, versioned problem-type URI.title- Short summary, stable perproblem_type.status- Numeric status class.detail- This-occurrence explanation.instance- URI identifying this occurrence.
§Returns
A new ProblemDetails with no extensions set.
Sourcepub const fn with_retry_after(self, seconds: u64) -> Self
pub const fn with_retry_after(self, seconds: u64) -> Self
Sets retry_after to seconds, marking the error as transient.
Sourcepub fn with_suggested_fix(self, fix: SuggestedFix) -> Self
pub fn with_suggested_fix(self, fix: SuggestedFix) -> Self
Attaches a SuggestedFix.
Sourcepub fn with_code_action(self, action: CodeAction) -> Self
pub fn with_code_action(self, action: CodeAction) -> Self
Appends a CodeAction to code_actions.
Sourcepub const fn with_exit_code(self, code: u8) -> Self
pub const fn with_exit_code(self, code: u8) -> Self
Sets the exit_code extension.
Sourcepub fn to_json(&self) -> String
pub fn to_json(&self) -> String
Serializes the envelope as a compact application/problem+json string.
§Returns
The compact JSON representation. Returns "{}" only if serialization
fails, which cannot happen for this all-owned, self-describing struct.
Sourcepub fn to_json_pretty(&self) -> String
pub fn to_json_pretty(&self) -> String
Serializes the envelope as pretty-printed application/problem+json.
§Returns
The indented JSON representation, suitable for human inspection.
Trait Implementations§
Source§impl Clone for ProblemDetails
impl Clone for ProblemDetails
Source§fn clone(&self) -> ProblemDetails
fn clone(&self) -> ProblemDetails
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ProblemDetails
impl Debug for ProblemDetails
Source§impl<'de> Deserialize<'de> for ProblemDetails
impl<'de> Deserialize<'de> for ProblemDetails
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>,
impl Eq for ProblemDetails
Source§impl PartialEq for ProblemDetails
impl PartialEq for ProblemDetails
Source§fn eq(&self, other: &ProblemDetails) -> bool
fn eq(&self, other: &ProblemDetails) -> bool
self and other values to be equal, and is used by ==.Source§impl Serialize for ProblemDetails
impl Serialize for ProblemDetails
impl StructuralPartialEq for ProblemDetails
Auto Trait Implementations§
impl Freeze for ProblemDetails
impl RefUnwindSafe for ProblemDetails
impl Send for ProblemDetails
impl Sync for ProblemDetails
impl Unpin for ProblemDetails
impl UnsafeUnpin for ProblemDetails
impl UnwindSafe for ProblemDetails
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.