Skip to content

Commit 078b112

Browse files
committed
add a panicking example
1 parent 4420cc3 commit 078b112

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

library/std/src/error.rs

+51-4
Original file line numberDiff line numberDiff line change
@@ -857,14 +857,61 @@ impl dyn Error + Send + Sync {
857857
///
858858
/// fn main() {
859859
/// match get_super_error() {
860-
/// Err(e) => {
861-
/// let report = Report::new(e).pretty(true);
862-
/// println!("Error: {}", report);
863-
/// }
860+
/// Err(e) => println!("Error: {}", Report::new(e)),
864861
/// _ => println!("No error"),
865862
/// }
866863
/// }
867864
/// ```
865+
///
866+
/// This example produces the following output:
867+
///
868+
/// ```console
869+
/// Error: SuperError is here!: SuperErrorSideKick is here!
870+
/// ```
871+
///
872+
/// Report prints the same output via `Display` and `Debug`, so it works well with
873+
/// [`unwrap`]/[`expect`]:
874+
///
875+
/// ```should_panic
876+
/// #![feature(error_reporter)]
877+
/// use std::error::Report;
878+
/// # use std::error::Error;
879+
/// # use std::fmt;
880+
/// # #[derive(Debug)]
881+
/// # struct SuperError {
882+
/// # source: SuperErrorSideKick,
883+
/// # }
884+
/// # impl fmt::Display for SuperError {
885+
/// # fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
886+
/// # write!(f, "SuperError is here!")
887+
/// # }
888+
/// # }
889+
/// # impl Error for SuperError {
890+
/// # fn source(&self) -> Option<&(dyn Error + 'static)> {
891+
/// # Some(&self.source)
892+
/// # }
893+
/// # }
894+
/// # #[derive(Debug)]
895+
/// # struct SuperErrorSideKick;
896+
/// # impl fmt::Display for SuperErrorSideKick {
897+
/// # fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
898+
/// # write!(f, "SuperErrorSideKick is here!")
899+
/// # }
900+
/// # }
901+
/// # impl Error for SuperErrorSideKick {}
902+
/// # fn get_super_error() -> Result<(), SuperError> {
903+
/// # Err(SuperError { source: SuperErrorSideKick })
904+
/// # }
905+
///
906+
/// get_super_error().map_err(Report::new).unwrap();
907+
/// ```
908+
///
909+
/// This example produces the following output:
910+
///
911+
/// ```console
912+
/// thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: SuperError is here!: SuperErrorSideKick is here!', src/error.rs:34:40
913+
/// note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
914+
/// ```
868915
#[unstable(feature = "error_reporter", issue = "90172")]
869916
pub struct Report<E> {
870917
/// The error being reported.

0 commit comments

Comments
 (0)