use anyhow::Error;
use std::fmt;
use std::sync::Arc;
use thiserror::Error;
#[derive(Clone, Error)]
pub struct ClonableError {
#[source]
err: Arc<Error>,
}
impl From<Error> for ClonableError {
fn from(err: Error) -> Self {
Self { err: Arc::new(err) }
}
}
impl fmt::Display for ClonableError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.err.fmt(f)
}
}
impl fmt::Debug for ClonableError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.err.fmt(f)
}
}
#[cfg(test)]
mod tests {
use super::*;
use anyhow::format_err;
use thiserror::Error;
#[derive(Debug, Error, Clone)]
#[error("\"{}\" happened: {}", msg, cause)]
struct FooError {
msg: String,
#[source]
cause: ClonableError,
}
#[test]
fn clone() {
let cause = format_err!("root cause").into();
let err = FooError { msg: "something bad happened".to_string(), cause };
let cloned_err = err.clone();
assert_eq!(format!("{:?}", err), format!("{:?}", cloned_err));
}
}