From 5901a8966711ff23f7d762251958e1adaf706aa3 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 21 Jan 2024 20:17:01 +0100 Subject: [PATCH 1/3] internal: Remove `match_prefix!` --- crates/teloxide-core/src/errors.rs | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/crates/teloxide-core/src/errors.rs b/crates/teloxide-core/src/errors.rs index 8741668d..9138a32d 100644 --- a/crates/teloxide-core/src/errors.rs +++ b/crates/teloxide-core/src/errors.rs @@ -88,28 +88,13 @@ impl AsResponseParameters for crate::RequestError { } } -macro_rules! match_prefix { - ("") => {{ - |data: &str| Some(data.to_owned()) - }}; - ($prefix:literal) => {{ - |data: &str| { - if data.starts_with($prefix) { - Some(data.to_owned()) - } else { - None - } - } - }}; -} - macro_rules! impl_api_error { ( $( #[$meta:meta] )* $vis:vis enum $ident:ident { $( $( #[$var_meta:meta] )* - $var_name:ident $( ($var_inner:ty) )? = $var_string:literal $(with $var_parser: block)? + $var_name:ident $( ($var_inner:ty) )? = $var_string:literal $(with $var_parser:expr)? ),* } ) => { @@ -616,7 +601,13 @@ impl_api_error! { /// 1. [`SendMessage`] /// /// [`SendMessage`]: crate::payloads::SendMessage - CantParseEntities(String) = "{0}" with {match_prefix!("Bad Request: can't parse entities")}, + CantParseEntities(String) = "{0}" with |text: &str| { + if text.starts_with("Bad Request: can't parse entities") { + Some(text.to_owned()) + } else { + None + } + }, /// Occurs when bot tries to use getUpdates while webhook is active. /// @@ -712,7 +703,7 @@ impl_api_error! { /// description of the error. /// /// [open an issue]: https://github.com/teloxide/teloxide/issues/new - Unknown(String) = "Unknown error: {0:?}" with {match_prefix!("")} + Unknown(String) = "Unknown error: {0:?}" with |text: &str| Some(text.to_owned()) } } From 1233b913ab3f4dca96116d29bea11bb0aaf17916 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Sun, 21 Jan 2024 20:27:25 +0100 Subject: [PATCH 2/3] Replace `ApiError::NotFound` with `InvalidToken` --- crates/teloxide-core/CHANGELOG.md | 3 +++ crates/teloxide-core/src/errors.rs | 24 +++++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/crates/teloxide-core/CHANGELOG.md b/crates/teloxide-core/CHANGELOG.md index f8d06ed7..100ef1f0 100644 --- a/crates/teloxide-core/CHANGELOG.md +++ b/crates/teloxide-core/CHANGELOG.md @@ -145,6 +145,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `can_edit_messages` - `can_pin_messages` - `can_manage_topics` +- `ApiError::NotFound` is replaced with `ApiError::InvalidToken` which correctly parses all currently known errors caused by invalid bot tokens ([#998][pr998]) + +[pr998]: https://github.com/teloxide/teloxide/pull/998 ### Added diff --git a/crates/teloxide-core/src/errors.rs b/crates/teloxide-core/src/errors.rs index 9138a32d..7e07dc8b 100644 --- a/crates/teloxide-core/src/errors.rs +++ b/crates/teloxide-core/src/errors.rs @@ -123,7 +123,7 @@ macro_rules! impl_api_error { where E: ::serde::de::Error, { - $(impl_api_error!(@de v, $var_name, $var_string $(, $var_parser)*);)* + $(impl_api_error!(@de v, $var_name $( ($var_inner) )?, $var_string $(, $var_parser)*);)* Err(E::unknown_variant(v, &[])) } } @@ -138,17 +138,22 @@ macro_rules! impl_api_error { } }; }; - (@de $value: ident, $variant: ident, $val: literal) => { + (@de $value:ident, $variant:ident, $val:literal) => { if $value == $val { return Ok(Self::Value::$variant) } }; - (@de $value: ident, $variant: ident, $val: literal, $block: expr) => { + (@de $value:ident, $variant:ident ($var_inner:ty), $val:literal, $block:expr) => { match $block($value) { Some(data) => return Ok(Self::Value::$variant(data)), _ => {} } }; + (@de $value:ident, $variant:ident, $val:literal, $block:expr) => { + if $block($value) { + return Ok(Self::Value::$variant); + } + }; } impl_api_error! { @@ -159,9 +164,12 @@ impl_api_error! { /// Occurs when the bot tries to send message to user who blocked the bot. BotBlocked = "Forbidden: bot was blocked by the user", - /// Occurs when the bot token is incorrect. - // FIXME: rename this to something akin "InvalidToken" - NotFound = "Unauthorized", + /// Occurs when the bot token is invalid. + // N.B. These errors are actually slightly different, "Unauthorized" is when the bot token + // is formatted mostly right, but is incorrect, whereas "Not Found" is when the url is + // not handled by TBA at all. From user POV both of those are "token is invalid", but + // there might be some cases where this is not right... + InvalidToken = "Invalid bot token" with |text: &str| text == "Unauthorized" || text == "Not Found", /// Occurs when bot tries to modify a message without modification content. /// @@ -804,7 +812,8 @@ mod tests { let cases = &[ ("{\"data\": \"Forbidden: bot was blocked by the user\"}", ApiError::BotBlocked), - ("{\"data\": \"Unauthorized\"}", ApiError::NotFound), + ("{\"data\": \"Unauthorized\"}", ApiError::InvalidToken), + ("{\"data\": \"Not Found\"}", ApiError::InvalidToken), ( "{\"data\": \"Bad Request: message is not modified: specified new message content \ and reply markup are exactly the same as a current content and reply markup of \ @@ -1019,6 +1028,7 @@ mod tests { ApiError::Unknown(_) => { format!("Unknown error: \"{raw}\"") } + ApiError::InvalidToken => "Invalid bot token".to_owned(), _ => raw, }; assert_eq!(parsed.to_string(), expected_error_message); From 8ffac3802c31f73457e2541bb47c012bc377e967 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 8 Feb 2024 20:32:45 +0100 Subject: [PATCH 3/3] fixup clippy --- crates/teloxide-core/src/errors.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/teloxide-core/src/errors.rs b/crates/teloxide-core/src/errors.rs index 7e07dc8b..08d140e8 100644 --- a/crates/teloxide-core/src/errors.rs +++ b/crates/teloxide-core/src/errors.rs @@ -144,12 +144,14 @@ macro_rules! impl_api_error { } }; (@de $value:ident, $variant:ident ($var_inner:ty), $val:literal, $block:expr) => { + #[allow(clippy::redundant_closure_call)] match $block($value) { Some(data) => return Ok(Self::Value::$variant(data)), _ => {} } }; (@de $value:ident, $variant:ident, $val:literal, $block:expr) => { + #[allow(clippy::redundant_closure_call)] if $block($value) { return Ok(Self::Value::$variant); }