pub trait ToProblem: Display {
// Required method
fn to_problem(&self) -> ProblemDetails;
// Provided method
fn render(&self, format: OutputFormat) -> String { ... }
}Expand description
Implemented by each crate’s own error enum to map it to a
ProblemDetails envelope.
Keeps error enums scoped to each crate’s own failure modes (this
workspace has no shared top-level error type) while sharing one envelope
shape across the workspace. Requires std::fmt::Display (already
derived by thiserror::Error on every implementing enum) so the default
ToProblem::render can reuse it for pretty output.
§Examples
use mif_problem::{Applicability, CodeAction, ProblemDetails, ProblemMeta, SuggestedFix, ToProblem};
#[derive(Debug, thiserror::Error)]
enum ExampleError {
#[error("input was empty")]
Empty,
}
impl ToProblem for ExampleError {
fn to_problem(&self) -> ProblemDetails {
let meta = ProblemMeta {
slug: "empty-input",
version: "v1",
title: "Empty input",
status: 400,
exit_code: 2,
};
meta.into_details(env!("CARGO_PKG_NAME"), self.to_string())
.with_suggested_fix(SuggestedFix::new(
"Supply a non-empty input.",
Applicability::MaybeIncorrect,
))
.with_code_action(CodeAction::new(
"Provide a value",
"quickfix",
Applicability::MaybeIncorrect,
))
}
}
let err = ExampleError::Empty;
assert_eq!(err.to_problem().status, 400);
assert_eq!(err.render(mif_problem::OutputFormat::Pretty), "Error: input was empty");Required Methods§
Sourcefn to_problem(&self) -> ProblemDetails
fn to_problem(&self) -> ProblemDetails
Maps self to a fully-populated ProblemDetails envelope.
Provided Methods§
Sourcefn render(&self, format: OutputFormat) -> String
fn render(&self, format: OutputFormat) -> String
Renders self for the given OutputFormat.
Pretty rendering is Error: {self}, matching this workspace’s
existing mif-cli/mif-mcp error text. JSON rendering is the
compact RFC 9457 envelope from ToProblem::to_problem.
§Arguments
format- The format to render.
§Returns
The rendered error string (without a trailing newline).
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".