make errors module pub and add errors::AsResponseParameters trait

This commit is contained in:
Maybe Waffle 2021-10-24 15:45:33 +03:00
parent 51d6800a32
commit 6a91c44836
3 changed files with 54 additions and 13 deletions

View file

@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## unreleased
### Added
- `errors` module and `errors::AsResponseParameters` trait
- `EditedMessageIsTooLong` error [#109][pr109]
- `UntilDate` enum and use it for `{Restricted, Banned}::until_date` ([#116][pr116])
- `Limits::messages_per_min_channel` ([#121][pr121])
[pr109]: https://github.com/teloxide/teloxide-core/pull/109
[pr116]: https://github.com/teloxide/teloxide-core/pull/116
[pr121]: https://github.com/teloxide/teloxide-core/pull/121
### Changed
- `RequestError::RetryAfter` now has a `u32` field instead of `i32`

View file

@ -3,18 +3,7 @@ use std::io;
use serde::Deserialize;
use thiserror::Error;
/// An error caused by downloading a file.
#[derive(Debug, Error)]
pub enum DownloadError {
/// A network error while downloading a file from Telegram.
#[error("A network error: {0}")]
// NOTE: this variant must not be created by anything except the From impl
Network(#[source] reqwest::Error),
/// An I/O error while writing a file to destination.
#[error("An I/O error: {0}")]
Io(#[from] std::io::Error),
}
use crate::types::ResponseParameters;
/// An error caused by sending a request to Telegram.
#[derive(Debug, Error)]
@ -57,6 +46,47 @@ pub enum RequestError {
Io(#[source] io::Error),
}
/// An error caused by downloading a file.
#[derive(Debug, Error)]
pub enum DownloadError {
/// A network error while downloading a file from Telegram.
#[error("A network error: {0}")]
// NOTE: this variant must not be created by anything except the From impl
Network(#[source] reqwest::Error),
/// An I/O error while writing a file to destination.
#[error("An I/O error: {0}")]
Io(#[from] std::io::Error),
}
pub trait AsResponseParameters {
fn response_parameters(&self) -> Option<ResponseParameters>;
fn retry_after(&self) -> Option<u32> {
self.response_parameters().and_then(|rp| match rp {
ResponseParameters::RetryAfter(n) => Some(n),
_ => None,
})
}
fn migrate_to_chat_id(&self) -> Option<i64> {
self.response_parameters().and_then(|rp| match rp {
ResponseParameters::MigrateToChatId(id) => Some(id),
_ => None,
})
}
}
impl AsResponseParameters for crate::RequestError {
fn response_parameters(&self) -> Option<ResponseParameters> {
match self {
&Self::RetryAfter(n) => Some(ResponseParameters::RetryAfter(n)),
&Self::MigrateToChatId(id) => Some(ResponseParameters::MigrateToChatId(id)),
_ => None,
}
}
}
/// A kind of an API error.
#[derive(Debug, Error, Deserialize, PartialEq, Hash, Eq, Clone)]
#[serde(field_identifier)]

View file

@ -101,6 +101,7 @@ pub use self::{
};
pub mod adaptors;
pub mod errors;
pub mod net;
pub mod payloads;
pub mod prelude;
@ -109,7 +110,6 @@ pub mod types;
// reexported
mod bot;
mod errors;
// implementation details
mod serde_multipart;