From fe75e0ff07ee669b5b96f0b33b18d038ea61b6b1 Mon Sep 17 00:00:00 2001 From: Waffle Date: Thu, 19 Sep 2019 01:59:16 +0300 Subject: [PATCH] Add `MigrateToChatId` and `RetryAfter` to `RequestError` --- src/core/network/mod.rs | 56 +++++++++++++++++++++++----------------- src/core/requests/mod.rs | 12 +++++++++ 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/core/network/mod.rs b/src/core/network/mod.rs index 244c6d63..b890a173 100644 --- a/src/core/network/mod.rs +++ b/src/core/network/mod.rs @@ -57,18 +57,7 @@ pub async fn request_multipart( ) .map_err(RequestError::InvalidJson)?; - match response { - TelegramResponse::Ok { result, .. } => Ok(result), - TelegramResponse::Err { - description, - error_code, - response_parameters: _, - .. - } => Err(RequestError::ApiError { - description, - status_code: StatusCode::from_u16(error_code).unwrap(), - }), - } + response.into() } pub async fn request_json( @@ -89,18 +78,7 @@ pub async fn request_json( ) .map_err(RequestError::InvalidJson)?; - match response { - TelegramResponse::Ok { result, .. } => Ok(result), - TelegramResponse::Err { - description, - error_code, - response_parameters: _, - .. - } => Err(RequestError::ApiError { - description, - status_code: StatusCode::from_u16(error_code).unwrap(), - }), - } + response.into() } #[derive(Deserialize)] @@ -124,6 +102,36 @@ enum TelegramResponse { }, } +impl Into> for TelegramResponse { + fn into(self) -> Result { + match self { + TelegramResponse::Ok { result, .. } => Ok(result), + TelegramResponse::Err { + description, + error_code, + response_parameters, + .. + } => { + if let Some(params) = response_parameters { + match params { + ResponseParameters::RetryAfter(i) => { + Err(RequestError::RetryAfter(i)) + } + ResponseParameters::MigrateToChatId(to) => { + Err(RequestError::MigrateToChatId(to)) + } + } + } else { + Err(RequestError::ApiError { + description, + status_code: StatusCode::from_u16(error_code).unwrap(), + }) + } + } + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/core/requests/mod.rs b/src/core/requests/mod.rs index a1dfa82f..aa6bb37f 100644 --- a/src/core/requests/mod.rs +++ b/src/core/requests/mod.rs @@ -16,6 +16,16 @@ pub enum RequestError { description: String, }, + /// The group has been migrated to a supergroup with the specified + /// identifier. + #[display(fmt = "The group has been migrated to a supergroup with id {id}", id = _0)] + MigrateToChatId(i64), + + /// In case of exceeding flood control, the number of seconds left to wait + /// before the request can be repeated + #[display(fmt = "Retry after {secs} seconds", secs = _0)] + RetryAfter(i32), + #[display(fmt = "Network error: {err}", err = _0)] NetworkError(reqwest::Error), @@ -27,6 +37,8 @@ impl std::error::Error for RequestError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { RequestError::ApiError { .. } => None, + RequestError::MigrateToChatId(_) => None, + RequestError::RetryAfter(_) => None, RequestError::NetworkError(err) => Some(err), RequestError::InvalidJson(err) => Some(err), }