use std::fmt;
use failure::{Backtrace, Context, Fail};
#[derive(Debug)]
pub struct Error {
inner: Context<ErrorKind>,
}
impl Fail for Error {
fn cause(&self) -> Option<&Fail> {
self.inner.cause()
}
fn backtrace(&self) -> Option<&Backtrace> {
self.inner.backtrace()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.inner, f)
}
}
impl Error {
pub fn kind(&self) -> ErrorKind {
*self.inner.get_context()
}
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
Error {
inner: Context::new(kind),
}
}
}
impl From<Context<ErrorKind>> for Error {
fn from(inner: Context<ErrorKind>) -> Error {
Error { inner: inner }
}
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
pub enum ErrorKind {
#[fail(display = "Wrong string format, couldn't parse as UTF8")]
UTF8Decode,
#[fail(display = "Telegram server responded with an error")]
Telegram,
#[fail(display = "Failed to read file for upload")]
TelegramFileRead,
#[fail(display = "There was an error initializing HTTPS")]
HttpsInitializeError,
#[fail(display = "There was an error fetching the content")]
Hyper,
#[fail(display = "There was an error parsing the URI")]
Uri,
#[fail(display = "Failed to read or write data")]
IO,
#[fail(display = "Failed to parse a JSON string")]
JsonParse,
#[fail(display = "Failed to serialize to JSON")]
JsonSerialize,
#[fail(display = "Json from server is malformed")]
Json,
#[fail(display = "Failed to create a channel")]
Channel,
#[fail(display = "Failed to create the interval timer")]
IntervalTimer,
#[fail(display = "Tokio library caused an error")]
Tokio,
#[fail(display = "Please specify a file")]
NoFile,
#[fail(display = "Expected JSON to be a Map, got something else")]
JsonNotMap,
#[fail(display = "Unknown error")]
Unknown,
}
#[derive(Debug, Fail)]
#[fail(display = "{}", message)]
pub struct TelegramError {
message: String,
}
impl TelegramError {
pub fn new(message: String) -> Self {
TelegramError { message }
}
}