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

/// Wraps several types of errors.
#[derive(Debug)]
pub struct Error {
    pub kind: ErrorKind,
    pub msg: String,
}

/// Defines error kind.
#[derive(Debug, PartialEq, Eq)]
pub enum ErrorKind {
    FlagsmithClientError,
    FlagsmithAPIError,
}
impl Error {
    pub fn new(kind: ErrorKind, msg: String) -> Error {
        Error { kind, msg }
    }
}
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.kind {
            ErrorKind::FlagsmithClientError => write!(f, "Flagsmith API error: {}", &self.msg),
            ErrorKind::FlagsmithAPIError => write!(f, "Flagsmith client error: {}", &self.msg),
        }
    }
}

impl From<url::ParseError> for Error {
    fn from(e: url::ParseError) -> Self {
        Error::new(ErrorKind::FlagsmithClientError, e.to_string())
    }
}

impl From<reqwest::Error> for Error {
    fn from(e: reqwest::Error) -> Self {
        Error::new(ErrorKind::FlagsmithAPIError, e.to_string())
    }
}

impl From<serde_json::Error> for Error {
    fn from(e: serde_json::Error) -> Self {
        Error::new(ErrorKind::FlagsmithAPIError, e.to_string())
    }
}