summaryrefslogtreecommitdiff
path: root/src/error.rs
blob: f981f14ac1e4331708a1bda86aff99b9a4480baf (plain) (blame)
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
use core::fmt;
use serde::Deserialize;

pub type MLE<T> = Result<T, MLError>;

#[derive(Debug, Deserialize)]
pub struct MLError {
    etype: ErrorType,
    message: String,
}

#[derive(Debug, Deserialize)]
pub enum ErrorType {
    ArgumentError,
    ArgumentCountError,
    ConfigError,
    DBError,
    ModError,
    LibToml,
    LibSql,
    LibReq,
    LibChrono,
    LibJson,
    IoError,
    Other,
}

impl std::error::Error for MLError {
    fn description(&self) -> &str {
        &self.message
    }
}

impl fmt::Display for MLError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.etype {
            ErrorType::ArgumentError => write!(f, "User input not accepted: {}", self.message),
            ErrorType::ArgumentCountError => write!(f, "Too many/too few arguments"),
            ErrorType::ConfigError => write!(f, "CONFIG"),
            ErrorType::DBError => write!(f, "Database: {}", self.message),
            ErrorType::ModError => write!(f, "Mod: {}", self.message),
            ErrorType::LibToml => write!(f, "TOML"),
            ErrorType::LibSql => write!(f, "SQL: {}", self.message),
            ErrorType::LibReq => write!(f, "REQWEST"),
            ErrorType::LibChrono => write!(f, "Chrono error: {}", self.message),
            ErrorType::LibJson => write!(f, "JSON: {}", self.message),
            ErrorType::IoError => write!(f, "IO"),
            ErrorType::Other => write!(f, "OTHER"),
        }
    }
}

impl From<reqwest::Error> for MLError {
    fn from(error: reqwest::Error) -> Self {
        Self {
            etype: ErrorType::LibReq,
            message: error.to_string(),
        }
    }
}

impl From<toml::de::Error> for MLError {
    fn from(error: toml::de::Error) -> Self {
        Self {
            etype: ErrorType::LibToml,
            message: error.to_string(),
        }
    }
}

impl From<rusqlite::Error> for MLError {
    fn from(error: rusqlite::Error) -> Self {
        Self {
            etype: ErrorType::LibSql,
            message: error.to_string(),
        }
    }
}

impl From<toml::ser::Error> for MLError {
    fn from(error: toml::ser::Error) -> Self {
        Self {
            etype: ErrorType::LibToml,
            message: error.to_string(),
        }
    }
}

impl From<chrono::ParseError> for MLError {
    fn from(error: chrono::ParseError) -> Self {
        Self {
            etype: ErrorType::LibChrono,
            message: error.to_string(),
        }
    }
}

impl From<std::io::Error> for MLError {
    fn from(error: std::io::Error) -> Self {
        Self {
            etype: ErrorType::IoError,
            message: error.to_string(),
        }
    }
}

impl From<serde_json::error::Error> for MLError {
    fn from(value: serde_json::error::Error) -> Self {
        Self {
            etype: ErrorType::LibJson,
            message: value.to_string(),
        }
    }
}

impl MLError {
    pub fn new(etype: ErrorType, message: &str) -> Self {
        Self {
            etype,
            message: String::from(message),
        }
    }
}