mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-20 13:59:00 +01:00
Merge pull request #998 from teloxide/tba-error
Replace `ApiError::NotFound` with `InvalidToken`
This commit is contained in:
commit
6f0861f3f6
2 changed files with 31 additions and 25 deletions
|
@ -145,6 +145,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- `can_edit_messages`
|
- `can_edit_messages`
|
||||||
- `can_pin_messages`
|
- `can_pin_messages`
|
||||||
- `can_manage_topics`
|
- `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
|
### Added
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
macro_rules! impl_api_error {
|
||||||
(
|
(
|
||||||
$( #[$meta:meta] )*
|
$( #[$meta:meta] )*
|
||||||
$vis:vis enum $ident:ident {
|
$vis:vis enum $ident:ident {
|
||||||
$(
|
$(
|
||||||
$( #[$var_meta:meta] )*
|
$( #[$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)?
|
||||||
),*
|
),*
|
||||||
}
|
}
|
||||||
) => {
|
) => {
|
||||||
|
@ -138,7 +123,7 @@ macro_rules! impl_api_error {
|
||||||
where
|
where
|
||||||
E: ::serde::de::Error,
|
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, &[]))
|
Err(E::unknown_variant(v, &[]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -158,12 +143,19 @@ macro_rules! impl_api_error {
|
||||||
return Ok(Self::Value::$variant)
|
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) => {
|
||||||
|
#[allow(clippy::redundant_closure_call)]
|
||||||
match $block($value) {
|
match $block($value) {
|
||||||
Some(data) => return Ok(Self::Value::$variant(data)),
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_api_error! {
|
impl_api_error! {
|
||||||
|
@ -174,9 +166,12 @@ impl_api_error! {
|
||||||
/// Occurs when the bot tries to send message to user who blocked the bot.
|
/// Occurs when the bot tries to send message to user who blocked the bot.
|
||||||
BotBlocked = "Forbidden: bot was blocked by the user",
|
BotBlocked = "Forbidden: bot was blocked by the user",
|
||||||
|
|
||||||
/// Occurs when the bot token is incorrect.
|
/// Occurs when the bot token is invalid.
|
||||||
// FIXME: rename this to something akin "InvalidToken"
|
// N.B. These errors are actually slightly different, "Unauthorized" is when the bot token
|
||||||
NotFound = "Unauthorized",
|
// 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.
|
/// Occurs when bot tries to modify a message without modification content.
|
||||||
///
|
///
|
||||||
|
@ -616,7 +611,13 @@ impl_api_error! {
|
||||||
/// 1. [`SendMessage`]
|
/// 1. [`SendMessage`]
|
||||||
///
|
///
|
||||||
/// [`SendMessage`]: crate::payloads::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.
|
/// Occurs when bot tries to use getUpdates while webhook is active.
|
||||||
///
|
///
|
||||||
|
@ -712,7 +713,7 @@ impl_api_error! {
|
||||||
/// description of the error.
|
/// description of the error.
|
||||||
///
|
///
|
||||||
/// [open an issue]: https://github.com/teloxide/teloxide/issues/new
|
/// [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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -813,7 +814,8 @@ mod tests {
|
||||||
|
|
||||||
let cases = &[
|
let cases = &[
|
||||||
("{\"data\": \"Forbidden: bot was blocked by the user\"}", ApiError::BotBlocked),
|
("{\"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 \
|
"{\"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 \
|
and reply markup are exactly the same as a current content and reply markup of \
|
||||||
|
@ -1028,6 +1030,7 @@ mod tests {
|
||||||
ApiError::Unknown(_) => {
|
ApiError::Unknown(_) => {
|
||||||
format!("Unknown error: \"{raw}\"")
|
format!("Unknown error: \"{raw}\"")
|
||||||
}
|
}
|
||||||
|
ApiError::InvalidToken => "Invalid bot token".to_owned(),
|
||||||
_ => raw,
|
_ => raw,
|
||||||
};
|
};
|
||||||
assert_eq!(parsed.to_string(), expected_error_message);
|
assert_eq!(parsed.to_string(), expected_error_message);
|
||||||
|
|
Loading…
Add table
Reference in a new issue