1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use crate::types::chat;
use hyper::Chunk;
use std::{
    error::Error,
    fmt::{self, Display, Formatter},
};

/// Represents possible errors that may happen during a method call.
#[derive(Debug)]
pub enum MethodCall {
    /// A network error.
    Network(hyper::Error),
    /// Bots API is likely to be down.
    OutOfService,
    /// Failed to parse the response.
    Parse {
        /// The response which failed to parse.
        response: Chunk,
        /// The error which parsing failed with.
        error: serde_json::Error,
    },
    /// An error returned in response.
    RequestError {
        /// A human-readable description of the error.
        description: String,
        /// The error code for this error.
        error_code: u16,
        /// The group moved to a supergroup with the following ID.
        migrate_to_chat_id: Option<chat::Id>,
        /// The bot exceeded flood threshold. You can make another request
        /// after the following amount of seconds.
        retry_after: Option<u64>,
    },
}

impl Display for MethodCall {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            MethodCall::Network(error) => write!(
                formatter,
                "A method call failed because of a network error: {}",
                error,
            ),
            MethodCall::OutOfService => write!(
                formatter,
                "A method call failed because Telegram is out of service.",
            ),
            MethodCall::Parse { response, error } => write!(
                formatter,
                "A method call failed because `tbot` failed to parse the \
                response.\n\n\

                The response was: {response:?}\n\
                The error was: {error}",
                response = response,
                error = error,
            ),
            MethodCall::RequestError {
                description,
                error_code,
                migrate_to_chat_id,
                retry_after,
            } => write!(
                formatter,
                "A method call failed because Telegram responded with an error \
                {error_code} `{description}`. Additional information:\n\n\

                - migrate_to_chat_id: {chat_id:?}\n\
                - retry_after: {retry_after:?}",
                error_code = error_code,
                description = description,
                chat_id = migrate_to_chat_id,
                retry_after = retry_after,
            ),
        }
    }
}

impl Error for MethodCall {}

impl MethodCall {
    /// Checks if `self` is `Network`.
    pub fn is_network(&self) -> bool {
        match self {
            MethodCall::Network(..) => true,
            _ => false,
        }
    }

    /// Checks if `self` is `OutOfService`.
    pub fn is_out_of_service(&self) -> bool {
        match self {
            MethodCall::OutOfService => true,
            _ => false,
        }
    }

    /// Checks if `self` is `Parse`.
    pub fn is_parse(&self) -> bool {
        match self {
            MethodCall::Parse { .. } => true,
            _ => false,
        }
    }

    /// Checks if `self` is `RequestError`.
    pub fn is_request_error(&self) -> bool {
        match self {
            MethodCall::RequestError { .. } => true,
            _ => false,
        }
    }
}

impl From<hyper::Error> for MethodCall {
    fn from(error: hyper::Error) -> Self {
        MethodCall::Network(error)
    }
}