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#[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#[derive(thiserror::Error, Debug, Serialize, Deserialize)]
36pub enum AckError {
37 #[error("cannot deserialize packet from ack response: {0:?}")]
39 Decode(#[from] ParserError),
40
41 #[error("ack timeout error")]
43 Timeout,
44
45 #[error("Error sending data through the engine.io socket: {0:?}")]
47 Socket(#[from] SocketError),
48}
49
50#[derive(thiserror::Error, Debug)]
52pub enum SendError {
53 #[error("Error serializing packet: {0:?}")]
55 Serialize(#[from] ParserError),
56
57 #[error("Error sending data through the engine.io socket: {0:?}")]
59 Socket(#[from] SocketError),
60}
61
62#[derive(thiserror::Error, Debug)]
64pub enum EmitWithAckError {
65 #[error("Error encoding data: {0:?}")]
67 Encode(#[from] ParserError),
68 #[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
79impl 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}