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
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::{
    error::Error as StdError,
    fmt::{self, Display, Formatter},
};

pub use crate::api::HmcParseError;
pub use hrpc::client::ClientError as InternalClientError;
pub use hrpc::url::ParseError as UrlError;
pub use reqwest::Error as ReqwestError;

/// Result type used by many `Client` methods.
pub type ClientResult<T> = Result<T, ClientError>;

/// Error type used by `Client`.
#[derive(Debug)]
pub enum ClientError {
    Internal(InternalClientError),
    /// Returned if an error occurs with the HTTP client.
    Reqwest(ReqwestError),
    /// Returned if an error occurs while creating HTTP requests / parsing for URLs.
    UrlParse(UrlError),
    /// Returned if an authentication session isn't in progress, but authentication step methods were called.
    NoAuthId,
    /// Returned if the client is unauthenticated, but an API endpoint requires authentication.
    Unauthenticated,
    /// Returned if a response from the server has invalid / empty value(s) according to the protocol.
    UnexpectedResponse(String),
}

impl ClientError {
    pub(crate) fn unexpected(msg: impl ToString) -> Self {
        ClientError::UnexpectedResponse(msg.to_string())
    }
}

impl Display for ClientError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            ClientError::Internal(err) => write!(f, "An internal error occured: {}", err),
            ClientError::Reqwest(reqwest_err) => write!(f, "An error occured in HTTP client, or request was unsuccessful: {}", reqwest_err),
            ClientError::UrlParse(err) => write!(f, "An error occured while parsing an URL: {}", err),
            ClientError::NoAuthId => write!(f, "No authentication session is in progress, but client tries to call auth API methods that need it"),
            ClientError::Unauthenticated => write!(f, "Client is not authenticated, but the API it tries to call requires authentication"),
            ClientError::UnexpectedResponse(msg) => write!(f, "Server responded with unexpected value: {}", msg),
        }
    }
}

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

impl From<UrlError> for ClientError {
    fn from(e: UrlError) -> Self {
        Self::UrlParse(e)
    }
}

impl From<InternalClientError> for ClientError {
    fn from(e: InternalClientError) -> Self {
        Self::Internal(e)
    }
}

impl StdError for ClientError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            ClientError::Internal(err) => Some(err),
            ClientError::Reqwest(err) => Some(err),
            ClientError::UrlParse(err) => Some(err),
            _ => None,
        }
    }
}