use std::error::Error as StdError;
use std::fmt::{self, Debug, Display};
#[derive(Debug, Copy, Clone)]
#[allow(clippy::exhaustive_structs)] pub struct Report<E>(pub E)
where
E: AsRef<dyn StdError>;
impl<E> Display for Report<E>
where
E: AsRef<dyn StdError>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn inner(mut e: &dyn StdError, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "error")?;
let mut last = String::new();
loop {
let this = e.to_string();
if !last.contains(&this) {
write!(f, ": {}", &this)?;
}
last = this;
if let Some(ne) = e.source() {
e = ne;
} else {
break;
}
}
Ok(())
}
inner(self.0.as_ref(), f)
}
}
#[allow(clippy::print_stderr)] pub fn report_and_exit<E, R>(e: E) -> R
where
E: AsRef<dyn StdError>,
{
fn eprint_progname() {
if let Some(progname) = std::env::args().next() {
eprint!("{}: ", progname);
}
}
eprint_progname();
eprintln!("{}", Report(e));
std::process::exit(127)
}
pub struct ReportHelper<'e>(&'e (dyn StdError + 'static));
impl<'e> AsRef<dyn StdError + 'static> for ReportHelper<'e> {
fn as_ref(&self) -> &(dyn StdError + 'static) {
self.0
}
}
pub trait ErrorReport: StdError + Sized + 'static {
fn report(&self) -> Report<ReportHelper> {
Report(ReportHelper(self as _))
}
}
impl<E: StdError + Sized + 'static> ErrorReport for E {}
#[macro_export]
macro_rules! define_asref_dyn_std_error { { $ty:ty } => {
impl AsRef<dyn std::error::Error + 'static> for $ty {
fn as_ref(&self) -> &(dyn std::error::Error + 'static) {
self as _
}
}
} }
#[cfg(test)]
mod test {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_duration_subtraction)]
use super::*;
use std::io;
use thiserror::Error;
#[derive(Error, Debug)]
#[error("terse")]
struct TerseError {
#[from]
source: Box<dyn StdError>,
}
#[derive(Error, Debug)]
#[error("verbose - {source}")]
struct VerboseError {
#[from]
source: Box<dyn StdError>,
}
#[derive(Error, Debug)]
#[error("shallow")]
struct ShallowError;
fn chk<E: StdError + 'static>(e: E, expected: &str) {
let e: Box<dyn StdError> = Box::new(e);
let got = Report(&e).to_string();
assert_eq!(got, expected, "\nmismatch: {:?}", &e);
}
#[test]
#[rustfmt::skip] fn test() {
chk(ShallowError,
"error: shallow");
let terse_1 = || TerseError { source: ShallowError.into() };
chk(terse_1(),
"error: terse: shallow");
let verbose_1 = || VerboseError { source: ShallowError.into() };
chk(verbose_1(),
"error: verbose - shallow");
chk(VerboseError { source: terse_1().into() },
"error: verbose - terse: shallow");
chk(TerseError { source: verbose_1().into() },
"error: terse: verbose - shallow");
chk(io::Error::new(io::ErrorKind::Other, ShallowError),
"error: shallow");
}
}