Add backtrace information to errors where relevant. (#166)

* Add backtrace information to errors where relevant.

* More backtrace information.

* Add to the FAQ.
This commit is contained in:
Laurent Mazare
2023-07-14 09:31:25 +01:00
committed by GitHub
parent a2f72edc0d
commit d88b6cdca9
11 changed files with 153 additions and 73 deletions

View File

@ -170,6 +170,12 @@ pub enum Error {
#[error(transparent)]
Wrapped(Box<dyn std::error::Error + Send + Sync>),
#[error("{inner}\n{backtrace}")]
WithBacktrace {
inner: Box<Self>,
backtrace: Box<std::backtrace::Backtrace>,
},
}
pub type Result<T> = std::result::Result<T, Error>;
@ -178,4 +184,16 @@ impl Error {
pub fn wrap(err: impl std::error::Error + Send + Sync + 'static) -> Self {
Self::Wrapped(Box::new(err))
}
pub fn bt(self) -> Self {
let backtrace = std::backtrace::Backtrace::capture();
match backtrace.status() {
std::backtrace::BacktraceStatus::Disabled
| std::backtrace::BacktraceStatus::Unsupported => self,
_ => Self::WithBacktrace {
inner: Box::new(self),
backtrace: Box::new(backtrace),
},
}
}
}