Implement Display, PartialEq, Eq and Error for RequestError

This commit is contained in:
Waffle 2019-09-02 16:33:32 +03:00
parent e1be16fba2
commit d72dae122d

View file

@ -27,16 +27,31 @@ fn file_url(base: &str, token: &str, file_path: &str) -> String {
) )
} }
#[derive(Debug)] #[derive(Debug, Display, PartialEq, Eq)]
pub enum RequestError { pub enum RequestError {
#[display(fmt = "Telegram error #{}: {}", status_code, description)]
ApiError { ApiError {
status_code: StatusCode, status_code: StatusCode,
description: String, description: String,
}, },
#[display(fmt = "Network error: {err}", err = _0)]
NetworkError(reqwest::Error), NetworkError(reqwest::Error),
#[display(fmt = "InvalidJson error caused by: {err}", err = _0)]
InvalidJson(serde_json::Error), InvalidJson(serde_json::Error),
} }
impl std::error::Error for RequestError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
RequestError::ApiError { .. } => None,
RequestError::NetworkError(err) => err,
RequestError::InvalidJson(err) => err,
}
}
}
pub type ResponseResult<T> = Result<T, RequestError>; pub type ResponseResult<T> = Result<T, RequestError>;
pub async fn request<T: DeserializeOwned, R: Request<ReturnValue = T>>( pub async fn request<T: DeserializeOwned, R: Request<ReturnValue = T>>(