web5_rust/
error.rs

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use snafu::Snafu;

fn get_backtrace() -> snafu::Backtrace {
    snafu::Backtrace::capture()
}

#[derive(Debug, Snafu)]
#[snafu(module)]
pub enum Error {
    #[snafu(transparent)]
    Hex{source: hex::FromHexError, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    Ed25519{source: ed25519_dalek::ed25519::Error, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    Base64Decode{source: base64::DecodeError, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    TryFromSlice{source: std::array::TryFromSliceError, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    Zbase32{source: zbase32::DecodeError},
    #[snafu(transparent)]
    SimpleCrypto{source: simple_crypto::Error, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    FromStringUtf8{source: std::string::FromUtf8Error, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    SimpleDns{source: simple_dns::SimpleDnsError, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    UrlParse{source: url::ParseError, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    SerdeJson{source: serde_json::Error, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    SimpleDatabase{source: simple_database::Error, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    Regex{source: regex::Error, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    Reqwest{source: reqwest::Error, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    SystemTime{source: std::time::SystemTimeError, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    SerdeBencode{source: serde_bencode::Error, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    Io{source: std::io::Error, backtrace: snafu::Backtrace},
    #[snafu(transparent)]
    Arc{source: std::sync::Arc<Error>},

    #[snafu(display("{message}"))]
    FailedDowncast{
        message: String,
        backtrace: snafu::Backtrace
    },
    #[snafu(display("Validation Error: {message}"))]
    Validation{message: String, backtrace: snafu::Backtrace},

    #[snafu(display("Could not parse type ({message}) from: {message1}"))]
    Parse{message: String, message1: String, backtrace: snafu::Backtrace},
    #[snafu(display("Invalid Authentication: ({message})"))]
    InvalidAuth{message: String, backtrace: snafu::Backtrace},
    #[snafu(display("Bad Response: {message}"))]
    BadResponse{message: String, backtrace: snafu::Backtrace},
    #[snafu(display("Bad Request: {message}"))]
    BadRequest{message: String, backtrace: snafu::Backtrace},
    #[snafu(display("Could Not Find: {message}"))]
    NotFound{message: String, backtrace: snafu::Backtrace},
    #[snafu(display("JsonRpc: {message}"))]
    JsonRpc{message: String, backtrace: snafu::Backtrace},

    #[snafu(display("Multi: {errors:?}"))]
    Multi{errors: Vec<Error>},

    #[snafu(display("InsufficentPermission"))]
    InsufficentPermission{backtrace: snafu::Backtrace},

    #[snafu(whatever)]
    Custom{message: String}
}

impl Error {
    pub fn custom(message: &str) -> Self {
        Error::Custom{message: message.to_string()}
    }

    pub fn bad_request(msg: &str) -> Self {
        Error::BadRequest{message: msg.to_string(), backtrace: get_backtrace()}
    }
    pub fn bad_response(msg: &str) -> Self {
        Error::BadResponse{message: msg.to_string(), backtrace: get_backtrace()}
    }
    pub fn invalid_auth(msg: &str) -> Self {
        Error::InvalidAuth{message: msg.to_string(), backtrace: get_backtrace()}
    }
    pub fn not_found(msg: &str) -> Self {
        Error::NotFound{message: msg.to_string(), backtrace: get_backtrace()}
    }
    pub fn json_rpc(msg: &str) -> Self {
        Error::JsonRpc{message: msg.to_string(), backtrace: get_backtrace()}
    }
    pub fn validation(msg: &str) -> Self {
        Error::Validation{message: msg.to_string(), backtrace: get_backtrace()}
    }

    pub fn multi(errors: Vec<Box<crate::agent::structs::ErrorWrapper>>) -> Self {
        errors.into()
    }

    pub fn insufficent_permission() -> Self {
        Error::InsufficentPermission{backtrace: get_backtrace()}
    }

    pub fn parse(r#type: &str, data: &str) -> Self {
        Error::Parse{message: r#type.to_string(), message1: data.to_string(), backtrace: get_backtrace()}
    }

    pub fn arc(err: std::sync::Arc<Error>) -> Self {
        Error::Arc{source: err}
    }
}

impl From<Vec<Box<crate::agent::structs::ErrorWrapper>>> for Error {
    fn from(mut errors: Vec<Box<crate::agent::structs::ErrorWrapper>>) -> Error {
        if errors.len() > 1 {
            Error::Multi{errors: errors.into_iter().map(|err| Error::arc((err).inner)).collect::<Vec<_>>()}
        } else {
            Error::arc((errors.remove(0)).inner)
        }
    }
}

impl From<Box<dyn crate::agent::traits::Response>> for Error {
    fn from(r: Box<dyn crate::agent::traits::Response>) -> Error {
        Error::FailedDowncast{
            message: format!("Tried to downcast {:?}: {} into something else", r, (*r).get_full_type()),
            backtrace: get_backtrace()
        }
    }
}