From ecc4743fdec43eb578e9c35bb008c68909f1517e Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 17:32:19 +0200 Subject: better error handling --- src/error.rs | 129 ----------------------------------------------------------- 1 file changed, 129 deletions(-) delete mode 100644 src/error.rs (limited to 'src/error.rs') 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 @@ -use core::fmt; -use serde::Deserialize; - -pub type MLE = Result; - -#[derive(Debug, Deserialize)] -pub struct MLErr { - etype: EType, - message: String, -} - -#[derive(Debug, Deserialize)] -pub enum EType { - ArgumentError, - ArgumentCountError, - ConfigError, - DBError, - ModError, - LibToml, - LibSql, - LibReq, - LibChrono, - LibJson, - LibIndicatif, - IoError, - Other, -} - -impl std::error::Error for MLErr { - fn description(&self) -> &str { - &self.message - } -} - -impl fmt::Display for MLErr { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self.etype { - EType::ArgumentError => { - write!(f, "User input not accepted: {}", self.message) - } - EType::ArgumentCountError => { - write!(f, "Too many/too few arguments") - } - EType::ConfigError => write!(f, "CONFIG"), - EType::DBError => write!(f, "Database: {}", self.message), - EType::ModError => write!(f, "Mod: {}", self.message), - EType::LibToml => write!(f, "TOML"), - EType::LibSql => write!(f, "SQL: {}", self.message), - EType::LibReq => write!(f, "REQWEST"), - EType::LibChrono => write!(f, "Chrono error: {}", self.message), - EType::LibJson => write!(f, "JSON: {}", self.message), - EType::LibIndicatif => write!(f, "Indicativ: {}", self.message), - EType::IoError => write!(f, "IO"), - EType::Other => write!(f, "OTHER"), - } - } -} - -impl From for MLErr { - fn from(error: reqwest::Error) -> Self { - Self { - etype: EType::LibReq, - message: error.to_string(), - } - } -} - -impl From for MLErr { - fn from(error: toml::de::Error) -> Self { - Self { - etype: EType::LibToml, - message: error.to_string(), - } - } -} - -impl From for MLErr { - fn from(error: rusqlite::Error) -> Self { - Self { - etype: EType::LibSql, - message: error.to_string(), - } - } -} - -impl From for MLErr { - fn from(error: toml::ser::Error) -> Self { - Self { - etype: EType::LibToml, - message: error.to_string(), - } - } -} - -impl From for MLErr { - fn from(error: chrono::ParseError) -> Self { - Self { - etype: EType::LibChrono, - message: error.to_string(), - } - } -} - -impl From for MLErr { - fn from(error: std::io::Error) -> Self { - Self { - etype: EType::IoError, - message: error.to_string(), - } - } -} - -impl From for MLErr { - fn from(value: serde_json::error::Error) -> Self { - Self { - etype: EType::LibJson, - message: value.to_string(), - } - } -} - -impl MLErr { - #[must_use] pub fn new(etype: EType, message: &str) -> Self { - Self { - etype, - message: String::from(message), - } - } -} -- cgit v1.2.3