diff options
author | fxqnlr <[email protected]> | 2024-09-04 17:32:19 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-09-04 17:32:19 +0200 |
commit | ecc4743fdec43eb578e9c35bb008c68909f1517e (patch) | |
tree | 73916114bc2bff8c72f759f5aae11a95d4dede22 /src/error.rs | |
parent | 11e64fc7560de3cd0def718edf68c31e3dc8be72 (diff) | |
download | modlist-refactor.tar modlist-refactor.tar.gz modlist-refactor.zip |
better error handlingrefactor
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 129 |
1 files changed, 0 insertions, 129 deletions
diff --git a/src/error.rs b/src/error.rs deleted file mode 100644 index 652fa0c..0000000 --- a/src/error.rs +++ /dev/null | |||
@@ -1,129 +0,0 @@ | |||
1 | use core::fmt; | ||
2 | use serde::Deserialize; | ||
3 | |||
4 | pub type MLE<T> = Result<T, MLErr>; | ||
5 | |||
6 | #[derive(Debug, Deserialize)] | ||
7 | pub struct MLErr { | ||
8 | etype: EType, | ||
9 | message: String, | ||
10 | } | ||
11 | |||
12 | #[derive(Debug, Deserialize)] | ||
13 | pub enum EType { | ||
14 | ArgumentError, | ||
15 | ArgumentCountError, | ||
16 | ConfigError, | ||
17 | DBError, | ||
18 | ModError, | ||
19 | LibToml, | ||
20 | LibSql, | ||
21 | LibReq, | ||
22 | LibChrono, | ||
23 | LibJson, | ||
24 | LibIndicatif, | ||
25 | IoError, | ||
26 | Other, | ||
27 | } | ||
28 | |||
29 | impl std::error::Error for MLErr { | ||
30 | fn description(&self) -> &str { | ||
31 | &self.message | ||
32 | } | ||
33 | } | ||
34 | |||
35 | impl fmt::Display for MLErr { | ||
36 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
37 | match self.etype { | ||
38 | EType::ArgumentError => { | ||
39 | write!(f, "User input not accepted: {}", self.message) | ||
40 | } | ||
41 | EType::ArgumentCountError => { | ||
42 | write!(f, "Too many/too few arguments") | ||
43 | } | ||
44 | EType::ConfigError => write!(f, "CONFIG"), | ||
45 | EType::DBError => write!(f, "Database: {}", self.message), | ||
46 | EType::ModError => write!(f, "Mod: {}", self.message), | ||
47 | EType::LibToml => write!(f, "TOML"), | ||
48 | EType::LibSql => write!(f, "SQL: {}", self.message), | ||
49 | EType::LibReq => write!(f, "REQWEST"), | ||
50 | EType::LibChrono => write!(f, "Chrono error: {}", self.message), | ||
51 | EType::LibJson => write!(f, "JSON: {}", self.message), | ||
52 | EType::LibIndicatif => write!(f, "Indicativ: {}", self.message), | ||
53 | EType::IoError => write!(f, "IO"), | ||
54 | EType::Other => write!(f, "OTHER"), | ||
55 | } | ||
56 | } | ||
57 | } | ||
58 | |||
59 | impl From<reqwest::Error> for MLErr { | ||
60 | fn from(error: reqwest::Error) -> Self { | ||
61 | Self { | ||
62 | etype: EType::LibReq, | ||
63 | message: error.to_string(), | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | |||
68 | impl From<toml::de::Error> for MLErr { | ||
69 | fn from(error: toml::de::Error) -> Self { | ||
70 | Self { | ||
71 | etype: EType::LibToml, | ||
72 | message: error.to_string(), | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | |||
77 | impl From<rusqlite::Error> for MLErr { | ||
78 | fn from(error: rusqlite::Error) -> Self { | ||
79 | Self { | ||
80 | etype: EType::LibSql, | ||
81 | message: error.to_string(), | ||
82 | } | ||
83 | } | ||
84 | } | ||
85 | |||
86 | impl From<toml::ser::Error> for MLErr { | ||
87 | fn from(error: toml::ser::Error) -> Self { | ||
88 | Self { | ||
89 | etype: EType::LibToml, | ||
90 | message: error.to_string(), | ||
91 | } | ||
92 | } | ||
93 | } | ||
94 | |||
95 | impl From<chrono::ParseError> for MLErr { | ||
96 | fn from(error: chrono::ParseError) -> Self { | ||
97 | Self { | ||
98 | etype: EType::LibChrono, | ||
99 | message: error.to_string(), | ||
100 | } | ||
101 | } | ||
102 | } | ||
103 | |||
104 | impl From<std::io::Error> for MLErr { | ||
105 | fn from(error: std::io::Error) -> Self { | ||
106 | Self { | ||
107 | etype: EType::IoError, | ||
108 | message: error.to_string(), | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | |||
113 | impl From<serde_json::error::Error> for MLErr { | ||
114 | fn from(value: serde_json::error::Error) -> Self { | ||
115 | Self { | ||
116 | etype: EType::LibJson, | ||
117 | message: value.to_string(), | ||
118 | } | ||
119 | } | ||
120 | } | ||
121 | |||
122 | impl MLErr { | ||
123 | #[must_use] pub fn new(etype: EType, message: &str) -> Self { | ||
124 | Self { | ||
125 | etype, | ||
126 | message: String::from(message), | ||
127 | } | ||
128 | } | ||
129 | } | ||