1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
// SPDX-License-Identifier: MIT OR Apache-2.0
//! # pretty-error-debug
//!
//! [](https://github.com/Kijewski/pretty-error-debug/actions/workflows/ci.yml)
//! [](https://crates.io/crates/pretty-error-debug)
//! 
//! [](/LICENSE-MIT "License: MIT OR Apache-2.0")
//!
//! Display a the chain of an error. Most useful as `Result<(), E>` for your `fn main()`,
//! and in conjunction with [`thiserror`](https://crates.io/crates/thiserror).
//!
//! This crate simply <del>plagiarized</del> <ins>extracted</ins> all the relevant formatting code from
//! [`anyhow`](https://crates.io/crates/anyhow).
//!
//! ## With `thiserror`
//!
//! ```ignore
//! #[derive(pretty_error_debug::Debug, thiserror::Error)]
//! pub enum MyError {
//! #[error("Error variant 1 happened")]
//! Variant1(#[from] Error1),
//! #[error("Error variant 2 happened")]
//! Variant2(#[from] Error2),
//! }
//!
//! fn main() -> Result<(), MyError> {
//! # /*
//! …
//! # */ Ok(())
//! }
//! ```
//!
//! ## Without `thiserror`
//!
//! ```
//! use std::error::Error;
//! use std::fmt::{self, Write};
//! #
//! # #[derive(Debug)] pub struct Error1;
//! # impl Error for Error1 {}
//! # impl fmt::Display for Error1 {
//! # fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//! # write!(f, "Error1")
//! # }
//! # }
//! #
//! # #[derive(Debug)] pub struct Error2;
//! # impl Error for Error2 {}
//! # impl fmt::Display for Error2 {
//! # fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//! # write!(f, "Error2")
//! # }
//! # }
//!
//! #[derive(pretty_error_debug::Debug)]
//! pub enum MyError {
//! Variant1(Error1),
//! Variant2(Error2),
//! }
//!
//! impl fmt::Display for MyError {
//! fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//! match self {
//! MyError::Variant1(_) => write!(f, "Error variant 1 happened"),
//! MyError::Variant2(_) => write!(f, "Error variant 2 happened"),
//! }
//! }
//! }
//!
//! impl Error for MyError {
//! fn source(&self) -> Option<&(dyn Error + 'static)> {
//! match self {
//! MyError::Variant1(source) => Some(source),
//! MyError::Variant2(source) => Some(source),
//! }
//! }
//! }
//!
//! fn main() -> Result<(), MyError> {
//! # /*
//! …
//! # */ Ok(())
//! }
//! ```
//!
#[cfg(test)]
mod test;
#[cfg(feature = "derive")]
extern crate pretty_error_debug_derive;
use std::error::Error;
use std::fmt;
use std::fmt::Write;
#[cfg(feature = "derive")]
pub use pretty_error_debug_derive::PrettyDebug as Debug;
// ////////////////////////////////////////////////////////////////////////////////////////////////
// All further code was extracted from:
// * https://github.com/dtolnay/anyhow/blob/0ba6408b5ef508c3dfc95797d21cfbdca9dd64ee/src/fmt.rs
// * https://github.com/dtolnay/anyhow/blob/fa9bcc0457a2e51593b874cc2f8bcb5608ad43fe/src/chain.rs
//
// Author: David Tolnay <dtolnay@gmail.com> and contributors to the `anyhow` project.
// ////////////////////////////////////////////////////////////////////////////////////////////////
/// Write out the [`Error`] message and chain.
///
/// Please see the [`crate`] documentation for a more complete example.
///
/// ```rust
/// use std::fmt::{self, Write};
///
/// pub enum MyError {
/// Variant1(/* … */),
/// Variant2(/* … */),
/// // …
/// }
///
/// impl fmt::Debug for MyError {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// # /*
/// pretty_error_debug::pretty_error_debug(self, f)
/// # */ Ok(())
/// }
/// }
///
/// // TODO: implement `std::fmt::Display` and `std::error::Error`.
/// ```
///
/// # Errors
///
/// Fails if writing to the `f` argument failed.
///
#[cold]
pub fn pretty_error_debug(error: &dyn Error, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", error)?;
if let Some(cause) = error.source() {
write!(f, "\n\nCaused by:")?;
let multiple = cause.source().is_some();
for (n, error) in Chain(Some(cause)).enumerate() {
writeln!(f)?;
let mut indented = Indented {
inner: f,
number: if multiple { Some(n + 1) } else { None },
started: false,
};
write!(indented, "{}", error)?;
}
}
Ok(())
}
struct Chain<'a>(Option<&'a dyn Error>);
impl<'a> Iterator for Chain<'a> {
type Item = &'a dyn Error;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let error = self.0?;
self.0 = error.source();
Some(error)
}
}
struct Indented<'a, 'b: 'a> {
inner: &'a mut fmt::Formatter<'b>,
number: Option<usize>,
started: bool,
}
impl<'a, 'b: 'a> fmt::Write for Indented<'a, 'b> {
fn write_str(&mut self, s: &str) -> fmt::Result {
for (i, line) in s.split('\n').enumerate() {
if !self.started {
self.started = true;
match self.number {
Some(number) => write!(self.inner, "{: >5}: ", number)?,
None => self.inner.write_str(" ")?,
}
} else if i > 0 {
self.inner.write_char('\n')?;
if self.number.is_some() {
self.inner.write_str(" ")?;
} else {
self.inner.write_str(" ")?;
}
}
self.inner.write_str(line)?;
}
Ok(())
}
}