pub struct Report<E = Box<dyn Error>> { /* private fields */ }
error_reporter
)std
only.Expand description
An error reporter that prints an error and its sources.
Report also exposes configuration options for formatting the error sources, either entirely on a single line, or in multi-line format with each source on a new line.
Report
only requires that the wrapped error implement Error
. It doesn’t require that the
wrapped error be Send
, Sync
, or 'static
.
§Examples
#![feature(error_reporter)]
use std::error::{Error, Report};
use std::fmt;
#[derive(Debug)]
struct SuperError {
source: 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.source)
}
}
#[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 { source: SuperErrorSideKick })
}
fn main() {
match get_super_error() {
Err(e) => println!("Error: {}", Report::new(e)),
_ => println!("No error"),
}
}
This example produces the following output:
Error: SuperError is here!: SuperErrorSideKick is here!
§Output consistency
Report prints the same output via Display
and Debug
, so it works well with
Result::unwrap
/Result::expect
which print their Err
variant via Debug
:
#![feature(error_reporter)]
use std::error::Report;
get_super_error().map_err(Report::new).unwrap();
This example produces the following output:
thread 'main' panicked at src/error.rs:34:40:
called `Result::unwrap()` on an `Err` value: SuperError is here!: SuperErrorSideKick is here!
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
§Return from main
Report
also implements From
for all types that implement Error
; this when combined with
the Debug
output means Report
is an ideal starting place for formatting errors returned
from main
.
#![feature(error_reporter)]
use std::error::Report;
fn main() -> Result<(), Report<SuperError>> {
get_super_error()?;
Ok(())
}
This example produces the following output:
Error: SuperError is here!: SuperErrorSideKick is here!
Note: Report
s constructed via ?
and From
will be configured to use the single line
output format. If you want to make sure your Report
s are pretty printed and include backtrace
you will need to manually convert and enable those flags.
#![feature(error_reporter)]
use std::error::Report;
fn main() -> Result<(), Report<SuperError>> {
get_super_error()
.map_err(Report::from)
.map_err(|r| r.pretty(true).show_backtrace(true))?;
Ok(())
}
This example produces the following output:
Error: SuperError is here!
Caused by:
SuperErrorSideKick is here!
Implementations§
Source§impl<E> Report<E>
impl<E> Report<E>
Sourcepub fn pretty(self, pretty: bool) -> Report<E>
🔬This is a nightly-only experimental API. (error_reporter
)
pub fn pretty(self, pretty: bool) -> Report<E>
error_reporter
)Enable pretty-printing the report across multiple lines.
§Examples
#![feature(error_reporter)]
use std::error::Report;
let error = SuperError { source: SuperErrorSideKick };
let report = Report::new(error).pretty(true);
eprintln!("Error: {report:?}");
This example produces the following output:
Error: SuperError is here!
Caused by:
SuperErrorSideKick is here!
When there are multiple source errors the causes will be numbered in order of iteration starting from the outermost error.
#![feature(error_reporter)]
use std::error::Report;
let source = SuperErrorSideKickSideKick;
let source = SuperErrorSideKick { source };
let error = SuperError { source };
let report = Report::new(error).pretty(true);
eprintln!("Error: {report:?}");
This example produces the following output:
Error: SuperError is here!
Caused by:
0: SuperErrorSideKick is here!
1: SuperErrorSideKickSideKick is here!
Sourcepub fn show_backtrace(self, show_backtrace: bool) -> Report<E>
🔬This is a nightly-only experimental API. (error_reporter
)
pub fn show_backtrace(self, show_backtrace: bool) -> Report<E>
error_reporter
)Display backtrace if available when using pretty output format.
§Examples
Note: Report will search for the first Backtrace
it can find starting from the
outermost error. In this example it will display the backtrace from the second error in the
sources, SuperErrorSideKick
.
#![feature(error_reporter)]
#![feature(error_generic_member_access)]
use std::error::Request;
use std::error::Report;
use std::backtrace::Backtrace;
#[derive(Debug)]
struct SuperErrorSideKick {
backtrace: Backtrace,
}
impl SuperErrorSideKick {
fn new() -> SuperErrorSideKick {
SuperErrorSideKick { backtrace: Backtrace::force_capture() }
}
}
impl Error for SuperErrorSideKick {
fn provide<'a>(&'a self, request: &mut Request<'a>) {
request.provide_ref::<Backtrace>(&self.backtrace);
}
}
// The rest of the example is unchanged ...
let source = SuperErrorSideKick::new();
let error = SuperError { source };
let report = Report::new(error).pretty(true).show_backtrace(true);
eprintln!("Error: {report:?}");
This example produces something similar to the following output:
Error: SuperError is here!
Caused by:
SuperErrorSideKick is here!
Stack backtrace:
0: rust_out::main::_doctest_main_src_error_rs_1158_0::SuperErrorSideKick::new
1: rust_out::main::_doctest_main_src_error_rs_1158_0
2: rust_out::main
3: core::ops::function::FnOnce::call_once
4: std::sys::backtrace::__rust_begin_short_backtrace
5: std::rt::lang_start::{{closure}}
6: std::panicking::try
7: std::rt::lang_start_internal
8: std::rt::lang_start
9: main
10: __libc_start_main
11: _start
Trait Implementations§
Auto Trait Implementations§
impl<E> Freeze for Report<E>where
E: Freeze,
impl<E> RefUnwindSafe for Report<E>where
E: RefUnwindSafe,
impl<E> Send for Report<E>where
E: Send,
impl<E> Sync for Report<E>where
E: Sync,
impl<E> Unpin for Report<E>where
E: Unpin,
impl<E> UnwindSafe for Report<E>where
E: UnwindSafe,
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize ⓘ
fn byte_align(&self) -> usize ⓘ
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T, R> Chain<R> for Twhere
T: ?Sized,
impl<T, R> Chain<R> for Twhere
T: ?Sized,
Source§impl<T> ExtAny for T
impl<T> ExtAny for T
Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§impl<T> ExtMem for Twhere
T: ?Sized,
impl<T> ExtMem for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of<T>() -> usize ⓘ
fn mem_align_of<T>() -> usize ⓘ
Source§fn mem_align_of_val(&self) -> usize ⓘ
fn mem_align_of_val(&self) -> usize ⓘ
Source§fn mem_size_of_val(&self) -> usize ⓘ
fn mem_size_of_val(&self) -> usize ⓘ
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true
if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self
without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice
only.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Hook for T
impl<T> Hook for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out
indicating that a T
is niched.