summaryrefslogblamecommitdiff
path: root/src/error.rs
blob: dbe7224555c9801896e1978806d6027f79a1f5be (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
              
                       


                                     
                             




                     
                             
                    
                  
                       
                
            
            

           
            
          










                                                          

                                                                                     
                                                          
                                                        
                                                    



                                                      



         





                                                                     





                                                                      





                                                                     











                                                                      




                                                         
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,
    LibToml,
    LibSql,
    LibReq,
    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, "Wrong argument"),
            ErrorType::ArgumentCountError => write!(f, "Too many/too few arguments"),
            ErrorType::ConfigError => write!(f, "CONFIG"),
            ErrorType::DBError => write!(f, "DATABASE"),
            ErrorType::LibToml => write!(f, "TOML"),
            ErrorType::LibSql => write!(f, "SQL"),
            ErrorType::LibReq => write!(f, "REQWEST"),
            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<std::io::Error> for MLError {
    fn from(error: std::io::Error) -> Self {
        Self { etype: ErrorType::IoError, message: error.to_string() }
    }
}

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