mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Use ChatId
and Duration
in errors
This commit is contained in:
parent
0e55ae79ee
commit
8e5146396c
4 changed files with 12 additions and 13 deletions
|
@ -202,7 +202,7 @@ where
|
||||||
|
|
||||||
let retry_after = res.as_ref().err().and_then(<_>::retry_after);
|
let retry_after = res.as_ref().err().and_then(<_>::retry_after);
|
||||||
if let Some(retry_after) = retry_after {
|
if let Some(retry_after) = retry_after {
|
||||||
let after = retry_after;
|
let after = retry_after.duration();
|
||||||
let until = Instant::now() + after;
|
let until = Instant::now() + after;
|
||||||
|
|
||||||
// If we'll retry, we check that worker hasn't died at the start of the loop
|
// If we'll retry, we check that worker hasn't died at the start of the loop
|
||||||
|
|
|
@ -316,7 +316,7 @@ async fn freeze(
|
||||||
match chat.slow_mode_delay() {
|
match chat.slow_mode_delay() {
|
||||||
Some(delay) => {
|
Some(delay) => {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let new_delay = Duration::from_secs(delay.into());
|
let new_delay = delay.duration();
|
||||||
slow_mode.insert(hash, (new_delay, now));
|
slow_mode.insert(hash, (new_delay, now));
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
//! Possible error types.
|
//! Possible error types.
|
||||||
|
|
||||||
use std::{io, time::Duration};
|
use std::io;
|
||||||
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::types::ResponseParameters;
|
use crate::types::{ChatId, ResponseParameters, Seconds};
|
||||||
|
|
||||||
/// An error caused by sending a request to Telegram.
|
/// An error caused by sending a request to Telegram.
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
|
@ -16,13 +16,12 @@ pub enum RequestError {
|
||||||
/// The group has been migrated to a supergroup with the specified
|
/// The group has been migrated to a supergroup with the specified
|
||||||
/// identifier.
|
/// identifier.
|
||||||
#[error("The group has been migrated to a supergroup with ID #{0}")]
|
#[error("The group has been migrated to a supergroup with ID #{0}")]
|
||||||
// FIXME: change to `ChatId` :|
|
MigrateToChatId(ChatId),
|
||||||
MigrateToChatId(i64),
|
|
||||||
|
|
||||||
/// In case of exceeding flood control, the number of seconds left to wait
|
/// In case of exceeding flood control, the number of seconds left to wait
|
||||||
/// before the request can be repeated.
|
/// before the request can be repeated.
|
||||||
#[error("Retry after {0:?}")]
|
#[error("Retry after {0}")]
|
||||||
RetryAfter(Duration),
|
RetryAfter(Seconds),
|
||||||
|
|
||||||
/// Network error while sending a request to Telegram.
|
/// Network error while sending a request to Telegram.
|
||||||
#[error("A network error: {0}")]
|
#[error("A network error: {0}")]
|
||||||
|
@ -64,14 +63,14 @@ pub enum DownloadError {
|
||||||
pub trait AsResponseParameters {
|
pub trait AsResponseParameters {
|
||||||
fn response_parameters(&self) -> Option<ResponseParameters>;
|
fn response_parameters(&self) -> Option<ResponseParameters>;
|
||||||
|
|
||||||
fn retry_after(&self) -> Option<Duration> {
|
fn retry_after(&self) -> Option<Seconds> {
|
||||||
self.response_parameters().and_then(|rp| match rp {
|
self.response_parameters().and_then(|rp| match rp {
|
||||||
ResponseParameters::RetryAfter(n) => Some(n),
|
ResponseParameters::RetryAfter(n) => Some(n),
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn migrate_to_chat_id(&self) -> Option<i64> {
|
fn migrate_to_chat_id(&self) -> Option<ChatId> {
|
||||||
self.response_parameters().and_then(|rp| match rp {
|
self.response_parameters().and_then(|rp| match rp {
|
||||||
ResponseParameters::MigrateToChatId(id) => Some(id),
|
ResponseParameters::MigrateToChatId(id) => Some(id),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|
|
@ -162,7 +162,7 @@ where
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::time::Duration;
|
use crate::types::{ChatId, Seconds};
|
||||||
|
|
||||||
use cool_asserts::assert_matches;
|
use cool_asserts::assert_matches;
|
||||||
|
|
||||||
|
@ -194,7 +194,7 @@ mod tests {
|
||||||
let json = r#"{"ok":false,"description":"this string is ignored","parameters":{"migrate_to_chat_id":123456}}"#.to_owned();
|
let json = r#"{"ok":false,"description":"this string is ignored","parameters":{"migrate_to_chat_id":123456}}"#.to_owned();
|
||||||
|
|
||||||
let res = deserialize_response::<True>(json);
|
let res = deserialize_response::<True>(json);
|
||||||
assert_matches!(res, Err(RequestError::MigrateToChatId(123456)));
|
assert_matches!(res, Err(RequestError::MigrateToChatId(ChatId(123456))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -202,7 +202,7 @@ mod tests {
|
||||||
let json = r#"{"ok":false,"description":"this string is ignored","parameters":{"retry_after":123456}}"#.to_owned();
|
let json = r#"{"ok":false,"description":"this string is ignored","parameters":{"retry_after":123456}}"#.to_owned();
|
||||||
|
|
||||||
let res = deserialize_response::<True>(json);
|
let res = deserialize_response::<True>(json);
|
||||||
assert_matches!(res, Err(RequestError::RetryAfter(duration)) if duration == Duration::from_secs(123456));
|
assert_matches!(res, Err(RequestError::RetryAfter(duration)) if duration == Seconds::from_seconds(123456));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue