Skip to main content

tbot/errors/
http_webhook.rs

1use crate::errors::MethodCall;
2use is_macro::Is;
3use std::{
4    error::Error,
5    fmt::{self, Display, Formatter},
6};
7use tokio::time::Elapsed;
8
9/// Represents possible errors that a webhook server may return.
10#[derive(Debug, Is)]
11pub enum HttpWebhook {
12    /// An error while setting the webhook.
13    SetWebhook(MethodCall),
14    /// Calling the `setWebhook` method timed out.
15    SetWebhookTimeout(Elapsed),
16    /// An error while running the server.
17    Server(hyper::Error),
18}
19
20impl Display for HttpWebhook {
21    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
22        match self {
23            Self::SetWebhook(error) => write!(
24                formatter,
25                "The webhook event loop failed because a call to `setWebhook` \
26                 failed with an error: {}",
27                error,
28            ),
29            Self::SetWebhookTimeout(timeout) => write!(
30                formatter,
31                "The webhook event loop failed because a call to `setWebhook`
32                timed out: {}",
33                timeout,
34            ),
35            Self::Server(error) => write!(
36                formatter,
37                "The webhook event loop failed because the server returned \
38                 an error: {}",
39                error,
40            ),
41        }
42    }
43}
44
45impl Error for HttpWebhook {}
46
47impl From<MethodCall> for HttpWebhook {
48    #[must_use]
49    fn from(error: MethodCall) -> Self {
50        Self::SetWebhook(error)
51    }
52}
53
54impl From<Elapsed> for HttpWebhook {
55    #[must_use]
56    fn from(timeout: Elapsed) -> Self {
57        Self::SetWebhookTimeout(timeout)
58    }
59}
60
61impl From<hyper::Error> for HttpWebhook {
62    #[must_use]
63    fn from(error: hyper::Error) -> Self {
64        Self::Server(error)
65    }
66}