socketioxide/
errors.rs

1use serde::{Deserialize, Serialize};
2use std::fmt::Debug;
3use tokio::time::error::Elapsed;
4
5pub use matchit::InsertError as NsInsertError;
6
7use engineioxide::socket::DisconnectReason as EIoDisconnectReason;
8use socketioxide_core::Sid;
9
10pub use crate::parser::ParserError;
11pub use socketioxide_core::adapter::errors::{AdapterError, BroadcastError, SocketError};
12
13/// Error type for socketio
14#[derive(thiserror::Error, Debug)]
15pub enum Error {
16    #[error("invalid packet type")]
17    InvalidPacketType,
18
19    #[error("invalid event name")]
20    InvalidEventName,
21
22    #[error("invalid namespace")]
23    InvalidNamespace,
24
25    #[error("cannot find socketio socket")]
26    SocketGone(Sid),
27
28    #[error("adapter error: {0}")]
29    Adapter(#[from] AdapterError),
30}
31
32pub(crate) struct ConnectFail;
33
34/// Error type for ack operations.
35#[derive(thiserror::Error, Debug, Serialize, Deserialize)]
36pub enum AckError {
37    /// The ack response cannot be parsed
38    #[error("cannot deserialize packet from ack response: {0:?}")]
39    Decode(#[from] ParserError),
40
41    /// The ack response timed out
42    #[error("ack timeout error")]
43    Timeout,
44
45    /// Error sending/receiving data through the engine.io socket
46    #[error("Error sending data through the engine.io socket: {0:?}")]
47    Socket(#[from] SocketError),
48}
49
50/// Error type for sending operations.
51#[derive(thiserror::Error, Debug)]
52pub enum SendError {
53    /// An error occurred while serializing the packet.
54    #[error("Error serializing packet: {0:?}")]
55    Serialize(#[from] ParserError),
56
57    /// Error sending/receiving data through the engine.io socket
58    #[error("Error sending data through the engine.io socket: {0:?}")]
59    Socket(#[from] SocketError),
60}
61
62/// Error type for the [`emit_with_ack`](crate::operators::BroadcastOperators::emit_with_ack) method.
63#[derive(thiserror::Error, Debug)]
64pub enum EmitWithAckError {
65    /// An error occurred while encoding the data.
66    #[error("Error encoding data: {0:?}")]
67    Encode(#[from] ParserError),
68    /// An error occurred while broadcasting to other nodes.
69    #[error("Adapter error: {0:?}")]
70    Adapter(#[from] Box<dyn std::error::Error + Send>),
71}
72
73impl From<Elapsed> for AckError {
74    fn from(_: Elapsed) -> Self {
75        Self::Timeout
76    }
77}
78
79/// Convert an [`Error`] to an [`EIoDisconnectReason`] if possible
80///
81/// If the error cannot be converted to a [`EIoDisconnectReason`] it means that the error was not fatal
82/// and the engine `Socket` can be kept alive
83impl From<&Error> for Option<EIoDisconnectReason> {
84    fn from(value: &Error) -> Self {
85        use EIoDisconnectReason::*;
86        match value {
87            Error::SocketGone(_) => Some(TransportClose),
88            Error::InvalidPacketType | Error::InvalidEventName => Some(PacketParsingError),
89            Error::Adapter(_) | Error::InvalidNamespace => None,
90        }
91    }
92}