Move RequestError to errors.rs

This commit is contained in:
Waffle 2019-09-21 00:06:35 +03:00
parent af9cf93c76
commit fb0daffbd5
5 changed files with 49 additions and 41 deletions

View file

@ -62,7 +62,7 @@ impl Bot {
/// types::File as TgFile,
/// };
/// use tokio::fs::File;
/// # use async_telegram_bot::requests::RequestError;
/// # use async_telegram_bot::RequestError;
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let bot = Bot::new("TOKEN");

View file

@ -1,3 +1,6 @@
use reqwest::StatusCode;
//<editor-fold desc="download">
#[derive(Debug, Display, From)]
pub enum DownloadError {
#[display(fmt = "Network error: {err}", err = _0)]
@ -15,3 +18,43 @@ impl std::error::Error for DownloadError {
}
}
}
//</editor-fold>
//<editor-fold desc="request">
#[derive(Debug, Display)]
pub enum RequestError {
#[display(fmt = "Telegram error #{}: {}", status_code, description)]
ApiError {
status_code: StatusCode,
description: String,
},
/// The group has been migrated to a supergroup with the specified
/// identifier.
#[display(fmt = "The group has been migrated to a supergroup with id {id}", id = _0)]
MigrateToChatId(i64),
/// In case of exceeding flood control, the number of seconds left to wait
/// before the request can be repeated
#[display(fmt = "Retry after {secs} seconds", secs = _0)]
RetryAfter(i32),
#[display(fmt = "Network error: {err}", err = _0)]
NetworkError(reqwest::Error),
#[display(fmt = "InvalidJson error caused by: {err}", err = _0)]
InvalidJson(serde_json::Error),
}
impl std::error::Error for RequestError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
RequestError::ApiError { .. } => None,
RequestError::MigrateToChatId(_) => None,
RequestError::RetryAfter(_) => None,
RequestError::NetworkError(err) => Some(err),
RequestError::InvalidJson(err) => Some(err),
}
}
}
//</editor-fold>

View file

@ -10,4 +10,4 @@ pub mod errors;
pub mod requests;
pub mod types;
pub use errors::DownloadError;
pub use errors::{DownloadError, RequestError};

View file

@ -1,5 +1,6 @@
use crate::{
requests::{RequestError, ResponseResult},
RequestError,
requests::ResponseResult,
types::ResponseParameters,
};

View file

@ -1,49 +1,13 @@
use std::future::Future;
use std::pin::Pin;
use reqwest::{r#async::Client, StatusCode};
use crate::errors::RequestError;
use reqwest::r#async::Client;
use serde::de::DeserializeOwned;
mod form_builder;
mod utils;
#[derive(Debug, Display)]
pub enum RequestError {
#[display(fmt = "Telegram error #{}: {}", status_code, description)]
ApiError {
status_code: StatusCode,
description: String,
},
/// The group has been migrated to a supergroup with the specified
/// identifier.
#[display(fmt = "The group has been migrated to a supergroup with id {id}", id = _0)]
MigrateToChatId(i64),
/// In case of exceeding flood control, the number of seconds left to wait
/// before the request can be repeated
#[display(fmt = "Retry after {secs} seconds", secs = _0)]
RetryAfter(i32),
#[display(fmt = "Network error: {err}", err = _0)]
NetworkError(reqwest::Error),
#[display(fmt = "InvalidJson error caused by: {err}", err = _0)]
InvalidJson(serde_json::Error),
}
impl std::error::Error for RequestError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
RequestError::ApiError { .. } => None,
RequestError::MigrateToChatId(_) => None,
RequestError::RetryAfter(_) => None,
RequestError::NetworkError(err) => Some(err),
RequestError::InvalidJson(err) => Some(err),
}
}
}
pub type ResponseResult<T> = Result<T, RequestError>;
/// Request that can be sent to telegram.