Expand description
Error
is a trait representing the basic expectations for error values,
i.e., values of type E
in Result<T, E>
. Errors must describe
themselves through the Display
and Debug
traits, and may provide
cause chain information:
Error::source()
is generally used when errors cross
“abstraction boundaries”. If one module must report an error that is caused
by an error from a lower-level module, it can allow accessing that error
via Error::source()
. This makes it possible for the high-level
module to provide its own errors while also revealing some of the
implementation for debugging via source
chains.
Provided methods
The lower-level source of this error, if any.
Examples
use std::error::Error;
use std::fmt;
#[derive(Debug)]
struct SuperError {
side: SuperErrorSideKick,
}
impl fmt::Display for SuperError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SuperError is here!")
}
}
impl Error for SuperError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.side)
}
}
#[derive(Debug)]
struct SuperErrorSideKick;
impl fmt::Display for SuperErrorSideKick {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SuperErrorSideKick is here!")
}
}
impl Error for SuperErrorSideKick {}
fn get_super_error() -> Result<(), SuperError> {
Err(SuperError { side: SuperErrorSideKick })
}
fn main() {
match get_super_error() {
Err(e) => {
println!("Error: {}", e);
println!("Caused by: {}", e.source().unwrap());
}
_ => println!("No error"),
}
}
Implementations
sourceimpl dyn Error + 'static
impl dyn Error + 'static
sourcepub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>
Returns some reference to the boxed value if it is of type T
, or
None
if it isn’t.
sourcepub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>
Returns some mutable reference to the boxed value if it is of type T
, or
None
if it isn’t.
sourceimpl dyn Error + Send + 'static
impl dyn Error + Send + 'static
sourcepub fn is<T: Error + 'static>(&self) -> bool
pub fn is<T: Error + 'static>(&self) -> bool
Forwards to the method defined on the type dyn Error
.
sourcepub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>
Forwards to the method defined on the type dyn Error
.
sourcepub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>
Forwards to the method defined on the type dyn Error
.
sourceimpl dyn Error + Send + Sync + 'static
impl dyn Error + Send + Sync + 'static
sourcepub fn is<T: Error + 'static>(&self) -> bool
pub fn is<T: Error + 'static>(&self) -> bool
Forwards to the method defined on the type dyn Error
.
sourcepub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T>
Forwards to the method defined on the type dyn Error
.
sourcepub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T>
Forwards to the method defined on the type dyn Error
.