mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-03 09:49:07 +01:00
Move RequestError
to errors.rs
This commit is contained in:
parent
af9cf93c76
commit
fb0daffbd5
5 changed files with 49 additions and 41 deletions
|
@ -62,7 +62,7 @@ impl Bot {
|
||||||
/// types::File as TgFile,
|
/// types::File as TgFile,
|
||||||
/// };
|
/// };
|
||||||
/// use tokio::fs::File;
|
/// use tokio::fs::File;
|
||||||
/// # use async_telegram_bot::requests::RequestError;
|
/// # use async_telegram_bot::RequestError;
|
||||||
///
|
///
|
||||||
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
|
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
/// let bot = Bot::new("TOKEN");
|
/// let bot = Bot::new("TOKEN");
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
use reqwest::StatusCode;
|
||||||
|
|
||||||
|
//<editor-fold desc="download">
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Display, From)]
|
||||||
pub enum DownloadError {
|
pub enum DownloadError {
|
||||||
#[display(fmt = "Network error: {err}", err = _0)]
|
#[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>
|
||||||
|
|
|
@ -10,4 +10,4 @@ pub mod errors;
|
||||||
pub mod requests;
|
pub mod requests;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
pub use errors::DownloadError;
|
pub use errors::{DownloadError, RequestError};
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
requests::{RequestError, ResponseResult},
|
RequestError,
|
||||||
|
requests::ResponseResult,
|
||||||
types::ResponseParameters,
|
types::ResponseParameters,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,49 +1,13 @@
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
|
||||||
use reqwest::{r#async::Client, StatusCode};
|
use crate::errors::RequestError;
|
||||||
|
use reqwest::r#async::Client;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
|
||||||
mod form_builder;
|
mod form_builder;
|
||||||
mod utils;
|
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>;
|
pub type ResponseResult<T> = Result<T, RequestError>;
|
||||||
|
|
||||||
/// Request that can be sent to telegram.
|
/// Request that can be sent to telegram.
|
||||||
|
|
Loading…
Reference in a new issue