use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use tokio::time::error::Elapsed;
pub use matchit::InsertError as NsInsertError;
use engineioxide::socket::DisconnectReason as EIoDisconnectReason;
use socketioxide_core::Sid;
pub use crate::parser::ParserError;
pub use socketioxide_core::adapter::errors::{AdapterError, BroadcastError, SocketError};
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("invalid packet type")]
InvalidPacketType,
#[error("invalid event name")]
InvalidEventName,
#[error("invalid namespace")]
InvalidNamespace,
#[error("cannot find socketio socket")]
SocketGone(Sid),
#[error("adapter error: {0}")]
Adapter(#[from] AdapterError),
}
pub(crate) struct ConnectFail;
#[derive(thiserror::Error, Debug, Serialize, Deserialize)]
pub enum AckError {
#[error("cannot deserialize packet from ack response: {0:?}")]
Decode(#[from] ParserError),
#[error("ack timeout error")]
Timeout,
#[error("Error sending data through the engine.io socket: {0:?}")]
Socket(#[from] SocketError),
}
#[derive(thiserror::Error, Debug)]
pub enum SendError {
#[error("Error serializing packet: {0:?}")]
Serialize(#[from] ParserError),
#[error("Error sending data through the engine.io socket: {0:?}")]
Socket(#[from] SocketError),
}
#[derive(thiserror::Error, Debug)]
pub enum EmitWithAckError {
#[error("Error encoding data: {0:?}")]
Encode(#[from] ParserError),
#[error("Adapter error: {0:?}")]
Adapter(#[from] Box<dyn std::error::Error + Send>),
}
impl From<Elapsed> for AckError {
fn from(_: Elapsed) -> Self {
Self::Timeout
}
}
impl From<&Error> for Option<EIoDisconnectReason> {
fn from(value: &Error) -> Self {
use EIoDisconnectReason::*;
match value {
Error::SocketGone(_) => Some(TransportClose),
Error::InvalidPacketType | Error::InvalidEventName => Some(PacketParsingError),
Error::Adapter(_) | Error::InvalidNamespace => None,
}
}
}