pub trait ErrorAttachment:
Any
+ Debug
+ Send
+ Sync {
// Provided method
fn fmt_context(&self, f: &mut Formatter<'_>) -> Result { ... }
}Expand description
Additional information attached to an Error.
Besides the location in the input, which is built into errors, layers
and other code can attach typed values to errors with
Error::set_attachment and retrieve them with
Error::attachment. An error holds at most one attachment per type.
For instance the deser-path crate attaches the path of the value an
error refers to.
Attachments can contribute to the Display output of
the error with fmt_context.
use std::fmt;
use deser::{Error, ErrorAttachment, ErrorKind};
#[derive(Debug)]
struct FileName(String);
impl ErrorAttachment for FileName {
fn fmt_context(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, " in {}", self.0)
}
}
let mut err = Error::with_position(ErrorKind::InvalidType, "unexpected string", 12, 2, 5);
err.set_attachment(FileName("config.json".into()));
assert_eq!(err.attachment::<FileName>().unwrap().0, "config.json");
assert_eq!(
err.to_string(),
"InvalidType: unexpected string at line 2 column 5 in config.json"
);Provided Methods§
Sourcefn fmt_context(&self, f: &mut Formatter<'_>) -> Result
fn fmt_context(&self, f: &mut Formatter<'_>) -> Result
Writes the attachment as part of the error message.
The output is appended to the message and the location of the error, so it typically starts with a space. By default attachments are not shown.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".