Use ChatId and Duration in errors

This commit is contained in:
Maybe Waffle 2023-02-21 18:03:43 +04:00
parent 0e55ae79ee
commit 8e5146396c
4 changed files with 12 additions and 13 deletions

View file

@ -202,7 +202,7 @@ where
let retry_after = res.as_ref().err().and_then(<_>::retry_after);
if let Some(retry_after) = retry_after {
let after = retry_after;
let after = retry_after.duration();
let until = Instant::now() + after;
// If we'll retry, we check that worker hasn't died at the start of the loop

View file

@ -316,7 +316,7 @@ async fn freeze(
match chat.slow_mode_delay() {
Some(delay) => {
let now = Instant::now();
let new_delay = Duration::from_secs(delay.into());
let new_delay = delay.duration();
slow_mode.insert(hash, (new_delay, now));
}
None => {

View file

@ -1,10 +1,10 @@
//! Possible error types.
use std::{io, time::Duration};
use std::io;
use thiserror::Error;
use crate::types::ResponseParameters;
use crate::types::{ChatId, ResponseParameters, Seconds};
/// An error caused by sending a request to Telegram.
#[derive(Debug, Error)]
@ -16,13 +16,12 @@ pub enum RequestError {
/// The group has been migrated to a supergroup with the specified
/// identifier.
#[error("The group has been migrated to a supergroup with ID #{0}")]
// FIXME: change to `ChatId` :|
MigrateToChatId(i64),
MigrateToChatId(ChatId),
/// In case of exceeding flood control, the number of seconds left to wait
/// before the request can be repeated.
#[error("Retry after {0:?}")]
RetryAfter(Duration),
#[error("Retry after {0}")]
RetryAfter(Seconds),
/// Network error while sending a request to Telegram.
#[error("A network error: {0}")]
@ -64,14 +63,14 @@ pub enum DownloadError {
pub trait AsResponseParameters {
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 {
ResponseParameters::RetryAfter(n) => Some(n),
_ => 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 {
ResponseParameters::MigrateToChatId(id) => Some(id),
_ => None,

View file

@ -162,7 +162,7 @@ where
#[cfg(test)]
mod tests {
use std::time::Duration;
use crate::types::{ChatId, Seconds};
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 res = deserialize_response::<True>(json);
assert_matches!(res, Err(RequestError::MigrateToChatId(123456)));
assert_matches!(res, Err(RequestError::MigrateToChatId(ChatId(123456))));
}
#[test]
@ -202,7 +202,7 @@ mod tests {
let json = r#"{"ok":false,"description":"this string is ignored","parameters":{"retry_after":123456}}"#.to_owned();
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]