mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Implement Display
, PartialEq
, Eq
and Error
for RequestError
This commit is contained in:
parent
e1be16fba2
commit
d72dae122d
1 changed files with 16 additions and 1 deletions
|
@ -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>>(
|
||||||
|
|
Loading…
Reference in a new issue