Skip to main content

ProblemDetails

Struct ProblemDetails 

Source
#[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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§problem_type: String

A URI reference identifying the problem type. Stable and versioned.

§title: String

Short, human-readable summary of the problem type. Stable per type.

§status: u16

Numeric status mapping to a status class (see also exit_code).

§detail: String

Human-readable explanation specific to this occurrence.

§instance: String

URI 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

Source

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 per problem_type.
  • status - Numeric status class.
  • detail - This-occurrence explanation.
  • instance - URI identifying this occurrence.
§Returns

A new ProblemDetails with no extensions set.

Source

pub const fn with_retry_after(self, seconds: u64) -> Self

Sets retry_after to seconds, marking the error as transient.

Source

pub fn with_suggested_fix(self, fix: SuggestedFix) -> Self

Attaches a SuggestedFix.

Source

pub fn with_code_action(self, action: CodeAction) -> Self

Appends a CodeAction to code_actions.

Source

pub const fn with_exit_code(self, code: u8) -> Self

Sets the exit_code extension.

Source

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.

Source

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

Source§

fn clone(&self) -> ProblemDetails

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ProblemDetails

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ProblemDetails

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for ProblemDetails

Source§

impl PartialEq for ProblemDetails

Source§

fn eq(&self, other: &ProblemDetails) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for ProblemDetails

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

impl StructuralPartialEq for ProblemDetails

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> 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.