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
use std::fmt;
use std::error;

///Generic Error type the encompasses all the errors this library can throw (Reqwest, Authentication error, etc.)
#[derive(Debug)]
pub enum SnError {
    ReqwestError(reqwest::Error),
    SnClientError(String),
}

impl fmt::Display for SnError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "SchoolNoose Client error")
    }
}

impl error::Error for SnError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        use SnError::*;
        Some(
            match self {
                ReqwestError(e) => {
                    e
                },
                SnClientError(_e) => {
                    return None;
                },
            }
        )
    }
}

impl From<reqwest::Error> for SnError {
    fn from(e: reqwest::Error) -> SnError {
        SnError::ReqwestError(e)
    }
}

impl From<std::num::ParseIntError> for SnError {
    fn from(_e: std::num::ParseIntError) -> SnError {
        SnError::SnClientError(String::from("Error parsing integer"))
    }
}