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
use std::any::Any;
use std::collections::BTreeMap;
use std::convert::Infallible;
use std::fmt::{Debug, Display, Formatter};
use std::sync::Arc;

pub struct Error {
    pub message: String,
    pub meta_map: BTreeMap<String, Arc<dyn Any + Send + Sync>>,
}

impl Error {

    pub fn new(message: impl Into<String>) -> Self {
        Self { message: message.into(), meta_map: BTreeMap::new() }
    }

    pub fn prefix(&self, prefix: impl AsRef<str>) -> Self {
        Self::new(format!("{}: {}", prefix.as_ref(), self.message))
    }

    pub fn message(&self) -> &str {
        self.message.as_str()
    }

    pub fn insert_meta<T: 'static + Send + Sync>(&mut self, key: impl Into<String>, val: T) {
        self.meta_map.insert(key.into(), Arc::new(val));
    }

    pub fn get_meta<T: 'static + Send>(&self, key: &str) -> Option<&T> {
        self.meta_map.get(key).and_then(|boxed| boxed.downcast_ref())
    }
}

impl Display for Error {

    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.message)
    }
}

impl Debug for Error {

    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.message)
    }
}

impl std::error::Error for Error { }

impl From<std::io::Error> for Error {

    fn from(value: std::io::Error) -> Self {
        Self::new(value.to_string())
    }
}

impl From<Infallible> for Error {
    fn from(_value: Infallible) -> Self {
        Self::new("infallible")
    }
}