use crate::types::chat;
use is_macro::Is;
use std::{
error::Error,
fmt::{self, Display, Formatter},
};
#[derive(Debug, Is)]
pub enum MethodCall {
Network(hyper::Error),
OutOfService,
Parse {
response: Vec<u8>,
error: serde_json::Error,
},
RequestError {
description: String,
error_code: u16,
migrate_to_chat_id: Option<chat::Id>,
retry_after: Option<u64>,
},
}
impl Display for MethodCall {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match self {
Self::Network(error) => write!(
formatter,
"A method call failed because of a network error: {}",
error,
),
Self::OutOfService => write!(
formatter,
"A method call failed because Telegram is out of service.",
),
Self::Parse { response, error } => write!(
formatter,
"A method call failed because `tbot` failed to parse the \
response.\n\n\
The response was: {response:?}\n\
The error was: {error}",
response = response,
error = error,
),
Self::RequestError {
description,
error_code,
migrate_to_chat_id,
retry_after,
} => write!(
formatter,
"A method call failed because Telegram responded with an error \
{error_code} `{description}`. Additional information:\n\n\
- migrate_to_chat_id: {chat_id:?}\n\
- retry_after: {retry_after:?}",
error_code = error_code,
description = description,
chat_id = migrate_to_chat_id,
retry_after = retry_after,
),
}
}
}
impl Error for MethodCall {}
impl From<hyper::Error> for MethodCall {
#[must_use]
fn from(error: hyper::Error) -> Self {
Self::Network(error)
}
}