telegram_bot_raw/requests/_base/
errors.rs1use std::error;
2use std::fmt;
3
4use crate::types::*;
5
6#[derive(Debug)]
7pub struct Error(ErrorKind);
8
9#[derive(Debug)]
10pub(crate) enum ErrorKind {
11 EmptyBody,
12 TelegramError {
13 description: String,
14 parameters: Option<ResponseParameters>,
15 },
16 DetachedError(String),
17 Json(::serde_json::Error),
18}
19
20impl From<::serde_json::Error> for ErrorKind {
21 fn from(error: ::serde_json::Error) -> Self {
22 ErrorKind::Json(error)
23 }
24}
25
26impl From<ErrorKind> for Error {
27 fn from(kind: ErrorKind) -> Self {
28 Error(kind)
29 }
30}
31
32impl fmt::Display for Error {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 match &self.0 {
35 ErrorKind::EmptyBody => write!(f, "empty body"),
36 ErrorKind::TelegramError {
37 description,
38 parameters,
39 } => {
40 f.write_str(&description)?;
41 if let Some(parameters) = parameters {
42 if let Some(chat_id) = parameters.migrate_to_chat_id {
43 write!(f, ", migrate to chat id: {}", chat_id)?;
44 }
45 if let Some(seconds) = parameters.retry_after {
46 write!(f, ", retry after: {}", seconds)?;
47 }
48 }
49 Ok(())
50 }
51 ErrorKind::DetachedError(s) => f.write_str(&s),
52 ErrorKind::Json(error) => write!(f, "{}", error),
53 }
54 }
55}
56
57impl error::Error for Error {}