Skip to main content

ferogram_connect/
error.rs

1// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
2//
3// ferogram: async Telegram MTProto client in Rust
4// https://github.com/ankit-chaubey/ferogram
5//
6// Licensed under either the MIT License or the Apache License 2.0.
7// See the LICENSE-MIT or LICENSE-APACHE file in this repository:
8// https://github.com/ankit-chaubey/ferogram
9//
10// Feel free to use, modify, and share this code.
11// Please keep this notice when redistributing.
12
13use std::{fmt, io};
14
15/// Errors produced by [`Connection`](crate::Connection) and transport helpers.
16#[derive(Debug)]
17pub enum ConnectError {
18    /// Network / I/O failure.
19    Io(io::Error),
20    /// Protocol violation or decoding failure.
21    Other(String),
22    /// Telegram transport-level error code (negative 4-byte word).
23    TransportCode(i32),
24    /// RPC error returned by Telegram (code + message string).
25    Rpc { code: i32, message: String },
26}
27
28impl ConnectError {
29    pub fn other(msg: impl Into<String>) -> Self {
30        Self::Other(msg.into())
31    }
32}
33
34impl fmt::Display for ConnectError {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Self::Io(e) => write!(f, "I/O error: {e}"),
38            Self::Other(s) => write!(f, "connect error: {s}"),
39            Self::TransportCode(c) => write!(f, "Telegram transport error: {c}"),
40            Self::Rpc { code, message } => write!(f, "RPC {code}: {message}"),
41        }
42    }
43}
44
45impl std::error::Error for ConnectError {
46    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
47        match self {
48            Self::Io(e) => Some(e),
49            _ => None,
50        }
51    }
52}
53
54impl From<io::Error> for ConnectError {
55    fn from(e: io::Error) -> Self {
56        Self::Io(e)
57    }
58}
59
60impl From<ferogram_tl_types::deserialize::Error> for ConnectError {
61    fn from(e: ferogram_tl_types::deserialize::Error) -> Self {
62        Self::Other(format!("TL deserialize error: {e:?}"))
63    }
64}