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

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("authentication required")]
    Auth,
    #[error("{0}")]
    Config(String),
    #[error("no parameters specified")]
    EmptyParams,
    #[error("invalid URL: {0}")]
    InvalidUrl(url::ParseError),
    #[error("{0}")]
    InvalidRequest(String),
    #[error("{0}")]
    InvalidValue(String),
    #[error("{0}")]
    IO(String),
    #[error("bugzilla: {message}")]
    Bugzilla { code: i64, message: String },
    #[error("redmine: {0}")]
    Redmine(String),
    #[error("{0}")]
    Request(reqwest::Error),
    #[error("request timed out")]
    Timeout,
    #[error("{0}")]
    Unsupported(String),
}

impl From<reqwest::Error> for Error {
    fn from(e: reqwest::Error) -> Self {
        // drop URL from error to avoid potentially leaking authentication parameters
        let e = e.without_url();
        if e.is_timeout() {
            Error::Timeout
        } else {
            Error::Request(e)
        }
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Error::IO(format!("{e}: {}", e.kind()))
    }
}

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