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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use anyhow::Error;
use std::fmt;
use std::sync::Arc;
use thiserror::Error;

/// A wrapper for `Error` that implements `Clone`.
#[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));
    }
}