use std::error::Error;
use std::fmt;
use anyerror::AnyError;
use crate::OptionalSend;
use crate::OptionalSync;
use crate::base::OptionalSerde;
pub trait ErrorSource: Error + Clone + PartialEq + Eq + OptionalSend + OptionalSync + OptionalSerde + 'static {
fn from_error<E: Error + 'static>(error: &E) -> Self;
fn from_string(msg: impl ToString) -> Self;
fn has_backtrace(&self) -> bool {
false
}
fn fmt_backtrace(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
}
}
impl ErrorSource for AnyError {
fn from_error<E: Error + 'static>(error: &E) -> Self {
AnyError::new(error)
}
fn from_string(msg: impl ToString) -> Self {
AnyError::error(msg)
}
fn has_backtrace(&self) -> bool {
self.backtrace().is_some()
}
fn fmt_backtrace(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(bt) = self.backtrace() {
write!(f, "{}", bt)
} else {
Ok(())
}
}
}
pub struct BacktraceDisplay<'a, E: ErrorSource>(pub &'a E);
impl<E: ErrorSource> fmt::Display for BacktraceDisplay<'_, E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0.has_backtrace() {
self.0.fmt_backtrace(f)
} else {
Ok(())
}
}
}
#[cfg(test)]
mod tests {
use anyerror::AnyError;
use super::BacktraceDisplay;
use super::ErrorSource;
#[cfg(feature = "serde")]
#[test]
fn test_any_error_reports_its_stored_backtrace() {
let json = serde_json::json!({
"typ": null, "msg": "read", "source": null, "context": [], "backtrace": "stored-marker"
});
let err: AnyError = serde_json::from_value(json).unwrap();
assert!(err.has_backtrace());
assert_eq!("stored-marker", BacktraceDisplay(&err).to_string());
}
#[test]
fn test_any_error_without_a_backtrace_reports_none() {
let err = AnyError::error("read");
assert_eq!(None, err.backtrace());
assert!(!err.has_backtrace());
assert_eq!("", BacktraceDisplay(&err).to_string());
}
}