aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: e5238c65c3c1064234e1111a5f0f1738c91e8f3b (plain) (blame)
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
48
49
50
51
52
53
54
use std::fmt;

/// The Errors that may occur around Clients.
#[derive(Debug)]
pub enum Error {
    ParseUrl(url::ParseError),
    Reqwest(reqwest::Error),
    SerdeXmlRpc(serde_xmlrpc::Error),
    Inexistent(String),
    MalformedResponse(serde_xmlrpc::Value),
    BadStatus(Vec<i32>, i32),
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::ParseUrl(e) => write!(fmt, "can't parse Url: {}", e),
            Error::Reqwest(e) => write!(fmt, "reqwest error: {}", e),
            Error::SerdeXmlRpc(e) => write!(fmt, "serde_xmlrpc error: {}", e),
            Error::Inexistent(what) => {
                write!(fmt, "parameter {} does not exist", what)
            }
            Error::MalformedResponse(resp) => {
                write!(fmt, "malformed response: {:?}", resp)
            }
            Error::BadStatus(expected, got) => {
                write!(fmt, "bad status {} (expected: {:?}", got, expected)
            }
        }
    }
}

impl From<url::ParseError> for Error {
    fn from(e: url::ParseError) -> Self {
        Self::ParseUrl(e)
    }
}

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

impl From<serde_xmlrpc::Error> for Error {
    fn from(e: serde_xmlrpc::Error) -> Self {
        Self::SerdeXmlRpc(e)
    }
}

/// A `Result` alias where the `Err` case is `inwx::Error`.
pub type Result<T> = std::result::Result<T, Error>;