forked from telegram-rs/telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
abolish ErrorKind, bring in thiserror, now all Error's can be matched
- Loading branch information
Showing
12 changed files
with
45 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,15 @@ | ||
use std::error; | ||
use std::fmt; | ||
|
||
#[derive(Debug)] | ||
pub struct Error(ErrorKind); | ||
|
||
#[derive(Debug)] | ||
pub enum ErrorKind { | ||
Raw(telegram_bot_raw::Error), | ||
Hyper(hyper::Error), | ||
Http(hyper::http::Error), | ||
Io(std::io::Error), | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("error from Hyper library")] | ||
Hyper(#[from] hyper::Error), | ||
#[error("http request error from Hyper library")] | ||
Http(#[from] hyper::http::Error), | ||
#[error("Invalid multipart filename")] | ||
InvalidMultipartFilename, | ||
#[error("ordinary IO Error")] | ||
Io(#[from] std::io::Error), | ||
#[error("raw error from Telegram API")] | ||
Raw(#[from] telegram_bot_raw::Error), | ||
} | ||
|
||
impl From<telegram_bot_raw::Error> for ErrorKind { | ||
fn from(error: telegram_bot_raw::Error) -> Self { | ||
ErrorKind::Raw(error) | ||
} | ||
} | ||
|
||
impl From<hyper::Error> for ErrorKind { | ||
fn from(error: hyper::Error) -> Self { | ||
ErrorKind::Hyper(error) | ||
} | ||
} | ||
|
||
impl From<hyper::http::Error> for ErrorKind { | ||
fn from(error: hyper::http::Error) -> Self { | ||
ErrorKind::Http(error) | ||
} | ||
} | ||
|
||
impl From<std::io::Error> for ErrorKind { | ||
fn from(error: std::io::Error) -> Self { | ||
ErrorKind::Io(error) | ||
} | ||
} | ||
|
||
impl From<ErrorKind> for Error { | ||
fn from(kind: ErrorKind) -> Self { | ||
Error(kind) | ||
} | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match &self.0 { | ||
ErrorKind::Raw(error) => write!(f, "{}", error), | ||
ErrorKind::Hyper(error) => write!(f, "{}", error), | ||
ErrorKind::Http(error) => write!(f, "{}", error), | ||
ErrorKind::Io(error) => write!(f, "{}", error), | ||
ErrorKind::InvalidMultipartFilename => write!(f, "invalid multipart filename"), | ||
} | ||
} | ||
} | ||
|
||
impl error::Error for Error {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,18 @@ | ||
use std::error; | ||
use std::fmt; | ||
use thiserror::Error; | ||
|
||
use crate::types::*; | ||
|
||
#[derive(Debug)] | ||
pub struct Error(ErrorKind); | ||
|
||
#[derive(Debug)] | ||
pub enum ErrorKind { | ||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
#[error("Emtpy body")] | ||
EmptyBody, | ||
#[error("Detached error")] | ||
DetachedError(String), | ||
#[error("JSON ser/de error")] | ||
Json(#[from] serde_json::Error), | ||
#[error("Telegram error")] | ||
TelegramError { | ||
description: String, | ||
parameters: Option<ResponseParameters>, | ||
}, | ||
DetachedError(String), | ||
Json(::serde_json::Error), | ||
} | ||
|
||
impl From<::serde_json::Error> for ErrorKind { | ||
fn from(error: ::serde_json::Error) -> Self { | ||
ErrorKind::Json(error) | ||
} | ||
} | ||
|
||
impl From<ErrorKind> for Error { | ||
fn from(kind: ErrorKind) -> Self { | ||
Error(kind) | ||
} | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match &self.0 { | ||
ErrorKind::EmptyBody => write!(f, "empty body"), | ||
ErrorKind::TelegramError { | ||
description, | ||
parameters, | ||
} => { | ||
f.write_str(description)?; | ||
if let Some(parameters) = parameters { | ||
if let Some(chat_id) = parameters.migrate_to_chat_id { | ||
write!(f, ", migrate to chat id: {}", chat_id)?; | ||
} | ||
if let Some(seconds) = parameters.retry_after { | ||
write!(f, ", retry after: {}", seconds)?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
ErrorKind::DetachedError(s) => f.write_str(s), | ||
ErrorKind::Json(error) => write!(f, "{}", error), | ||
} | ||
} | ||
} | ||
|
||
impl error::Error for Error {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters