mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-10 20:12:25 +01:00
make errors
module pub and add errors::AsResponseParameters
trait
This commit is contained in:
parent
51d6800a32
commit
6a91c44836
3 changed files with 54 additions and 13 deletions
11
CHANGELOG.md
11
CHANGELOG.md
|
@ -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`
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue