flagsmith_async/
error.rs

1use std::convert::From;
2use std::fmt;
3
4/// Wraps several types of errors.
5#[derive(Debug)]
6pub struct Error {
7    pub kind: ErrorKind,
8    pub msg: String,
9}
10
11/// Defines error kind.
12#[derive(Debug, PartialEq, Eq)]
13pub enum ErrorKind {
14    FlagsmithClientError,
15    FlagsmithAPIError,
16}
17impl Error {
18    pub fn new(kind: ErrorKind, msg: String) -> Error {
19        Error { kind, msg }
20    }
21}
22impl fmt::Display for Error {
23    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24        match self.kind {
25            ErrorKind::FlagsmithClientError => write!(f, "Flagsmith API error: {}", &self.msg),
26            ErrorKind::FlagsmithAPIError => write!(f, "Flagsmith client error: {}", &self.msg),
27        }
28    }
29}
30
31impl From<url::ParseError> for Error {
32    fn from(e: url::ParseError) -> Self {
33        Error::new(ErrorKind::FlagsmithClientError, e.to_string())
34    }
35}
36
37impl From<reqwest::Error> for Error {
38    fn from(e: reqwest::Error) -> Self {
39        Error::new(ErrorKind::FlagsmithAPIError, e.to_string())
40    }
41}
42
43impl From<serde_json::Error> for Error {
44    fn from(e: serde_json::Error) -> Self {
45        Error::new(ErrorKind::FlagsmithAPIError, e.to_string())
46    }
47}