aboutsummaryrefslogtreecommitdiff
path: root/src/error/mod.rs
blob: b8a8c163bb9dc1ce9c96daba9eeb07f12aa0db7d (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
55
56
57
58
59
60
61
62
63
use std::fmt;

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

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

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::ParseUrl(err) => write!(fmt, "can't parse Url: {}", err),
            Error::Reqwest(err) => write!(fmt, "reqwest error: {}", err),
            Error::XmlRpc(err) => write!(fmt, "xmlrpc error: {}", err),
            Error::Inexistent(what) => {
                write!(fmt, "parameter {} does not exist", what)
            }
            Error::Type(what, exp, got) => {
                write!(
                    fmt,
                    "parameter {what} is of wrong type {got:?} (expected: {exp})"
                )
            }
            Error::BadResponse(resp) => write!(fmt, "bad response: {:?}", resp),
            Error::BadStatus(expected, got) => {
                write!(fmt, "bad status {} (expected: {:?}", got, expected)
            }
            Error::BadVariant(ename, var) => {
                write!(fmt, "{} is not a valid enum variant for {}", var, ename)
            }
        }
    }
}

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

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

impl From<xmlrpc::Error> for Error {
    fn from(err: xmlrpc::Error) -> Self {
        Self::XmlRpc(err)
    }
}

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