1use std::time::Duration;
2
3#[derive(Debug, thiserror::Error)]
5#[non_exhaustive]
6pub enum TelegramError {
7 #[error("{0}")]
9 Forbidden(String),
10
11 #[error("{0}")]
13 InvalidToken(String),
14
15 #[error("{0}")]
17 EndPointNotFound(String),
18
19 #[error("{0}")]
21 Network(String),
22
23 #[error("{0}")]
25 BadRequest(String),
26
27 #[error("{0}")]
29 TimedOut(String),
30
31 #[error("Group migrated to supergroup. New chat id: {new_chat_id}")]
33 ChatMigrated {
34 new_chat_id: i64,
36 },
37
38 #[error("Flood control exceeded. Retry in {retry_after:?}")]
40 RetryAfter {
41 retry_after: Duration,
43 },
44
45 #[error("{0}")]
47 Conflict(String),
48
49 #[error("PassportDecryptionError: {0}")]
51 PassportDecryption(String),
52
53 #[error("HTTP error: {0}")]
55 Http(#[from] reqwest::Error),
56
57 #[error("JSON error: {0}")]
59 Json(#[from] serde_json::Error),
60
61 #[error("IO error: {0}")]
63 Io(#[from] std::io::Error),
64}
65
66impl TelegramError {
67 pub fn from_api_message(message: &str) -> String {
69 let msg = message
70 .strip_prefix("Error: ")
71 .or_else(|| message.strip_prefix("[Error]: "))
72 .or_else(|| message.strip_prefix("Bad Request: "));
73
74 match msg {
75 Some(stripped) => {
76 let mut chars = stripped.chars();
77 match chars.next() {
78 Some(c) => c.to_uppercase().to_string() + chars.as_str(),
79 None => String::new(),
80 }
81 }
82 None => message.to_string(),
83 }
84 }
85}
86
87pub type Result<T> = std::result::Result<T, TelegramError>;