From d72dae122d787c9ed1d19ffd861e101df9311062 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 2 Sep 2019 16:33:32 +0300 Subject: [PATCH 1/2] Implement `Display`, `PartialEq`, `Eq` and `Error` for RequestError --- src/core/network/mod.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/core/network/mod.rs b/src/core/network/mod.rs index 07d90391..c8fd8afd 100644 --- a/src/core/network/mod.rs +++ b/src/core/network/mod.rs @@ -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 { + #[display(fmt = "Telegram error #{}: {}", status_code, description)] ApiError { status_code: StatusCode, description: String, }, + + #[display(fmt = "Network error: {err}", err = _0)] NetworkError(reqwest::Error), + + #[display(fmt = "InvalidJson error caused by: {err}", err = _0)] 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 = Result; pub async fn request>( From 3e0e3ae4b478119a205ccd661ac218840d08fbb9 Mon Sep 17 00:00:00 2001 From: Waffle Date: Mon, 2 Sep 2019 16:35:52 +0300 Subject: [PATCH 2/2] Add docs for `method_url` and `file_url` --- src/core/network/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core/network/mod.rs b/src/core/network/mod.rs index c8fd8afd..1e58da23 100644 --- a/src/core/network/mod.rs +++ b/src/core/network/mod.rs @@ -9,6 +9,7 @@ use super::requests::Request; const TELEGRAM_API_URL: &str = "https://api.telegram.org"; +/// Create url for macking requests, see [telegram docs](https://core.telegram.org/bots/api#making-requests) fn method_url(base: &str, token: &str, method_name: &str) -> String { format!( "{url}/bot{token}/{method}", @@ -18,6 +19,7 @@ fn method_url(base: &str, token: &str, method_name: &str) -> String { ) } +/// Create url for downloading file, see [telegram docs](https://core.telegram.org/bots/api#file) fn file_url(base: &str, token: &str, file_path: &str) -> String { format!( "{url}/file/bot{token}/{file}",