Skip to main content

telebot/
error.rs

1use std::fmt;
2
3use failure::{Backtrace, Context, Fail};
4
5#[derive(Debug)]
6pub struct Error {
7    inner: Context<ErrorKind>,
8}
9
10impl Fail for Error {
11    fn cause(&self) -> Option<&Fail> {
12        self.inner.cause()
13    }
14
15    fn backtrace(&self) -> Option<&Backtrace> {
16        self.inner.backtrace()
17    }
18}
19
20impl fmt::Display for Error {
21    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22        fmt::Display::fmt(&self.inner, f)
23    }
24}
25
26impl Error {
27    pub fn kind(&self) -> ErrorKind {
28        *self.inner.get_context()
29    }
30}
31
32impl From<ErrorKind> for Error {
33    fn from(kind: ErrorKind) -> Error {
34        Error {
35            inner: Context::new(kind),
36        }
37    }
38}
39
40impl From<Context<ErrorKind>> for Error {
41    fn from(inner: Context<ErrorKind>) -> Error {
42        Error { inner: inner }
43    }
44}
45
46#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
47pub enum ErrorKind {
48    // indicates that the received reply couldn't be decoded (e.g. caused by an aborted
49    // connection)
50    #[fail(display = "Wrong string format, couldn't parse as UTF8")]
51    UTF8Decode,
52
53    // indicates a Telegram error (e.g. a property is missing)
54    #[fail(display = "Telegram server responded with an error")]
55    Telegram,
56
57    #[fail(display = "Failed to read file for upload")]
58    TelegramFileRead,
59
60    #[fail(display = "There was an error initializing HTTPS")]
61    HttpsInitializeError,
62
63    // indicates some failure in Hyper, missing network connection, etc.
64    #[fail(display = "There was an error fetching the content")]
65    Hyper,
66
67    // indicates some failure with parsing a URI
68    #[fail(display = "There was an error parsing the URI")]
69    Uri,
70
71    // indicates an error reading or writing data
72    #[fail(display = "Failed to read or write data")]
73    IO,
74
75    // indicates a malformated reply, this should never happen unless the Telegram server has a
76    // hard time
77    #[fail(display = "Failed to parse a JSON string")]
78    JsonParse,
79
80    #[fail(display = "Failed to serialize to JSON")]
81    JsonSerialize,
82
83    #[fail(display = "Json from server is malformed")]
84    Json,
85
86    #[fail(display = "Failed to create a channel")]
87    Channel,
88
89    #[fail(display = "Failed to create the interval timer")]
90    IntervalTimer,
91
92    #[fail(display = "Tokio library caused an error")]
93    Tokio,
94
95    #[fail(display = "Please specify a file")]
96    NoFile,
97
98    #[fail(display = "Expected JSON to be a Map, got something else")]
99    JsonNotMap,
100
101    // indicates an unknown error
102    #[fail(display = "Unknown error")]
103    Unknown,
104}
105
106#[derive(Debug, Fail)]
107#[fail(display = "{}", message)]
108pub struct TelegramError {
109    message: String,
110}
111
112impl TelegramError {
113    pub fn new(message: String) -> Self {
114        TelegramError { message }
115    }
116}