splited ApiErrorKind at known and unknown

This commit is contained in:
p0lunin 2020-07-06 15:54:05 +03:00
parent fe8146767e
commit 0393a8b53c
2 changed files with 14 additions and 3 deletions

View file

@ -36,9 +36,19 @@ pub enum RequestError {
InvalidJson(#[source] serde_json::Error),
}
/// A kind of an API error returned from Telegram.
#[derive(Debug, Deserialize, PartialEq, Copy, Hash, Eq, Clone)]
/// A kind of an API error returned from Telegram. If you receive Unknown value, please
/// [open the issue](https://github.com/teloxide/teloxide/issues/new) with description
/// of error.
#[derive(Debug, Deserialize, PartialEq, Hash, Eq, Clone)]
#[serde(untagged)]
pub enum ApiErrorKind {
Known(ApiErrorKindKnown),
Unknown(String)
}
/// A kind of an known API error returned from Telegram.
#[derive(Debug, Deserialize, PartialEq, Copy, Hash, Eq, Clone)]
pub enum ApiErrorKindKnown {
/// Occurs when the bot tries to send message to user who blocked the bot.
#[serde(rename = "Forbidden: bot was blocked by the user")]
BotBlocked,

View file

@ -63,10 +63,11 @@ impl<R> Into<ResponseResult<R>> for TelegramResponse<R> {
mod tests {
use super::*;
use crate::types::Update;
use crate::errors::ApiErrorKindKnown;
#[test]
fn terminated_by_other_get_updates() {
let expected = ApiErrorKind::TerminatedByOtherGetUpdates;
let expected = ApiErrorKind::Known(ApiErrorKindKnown::TerminatedByOtherGetUpdates);
if let TelegramResponse::Err{ kind, .. } = serde_json::from_str::<TelegramResponse<Update>>(r#"{"ok":false,"error_code":409,"description":"Conflict: terminated by other getUpdates request; make sure that only one bot instance is running"}"#).unwrap() {
assert_eq!(expected, kind);
}