socketioxide_core/adapter/
errors.rs

1//! All the errors that can be returned by the crate. Mostly when using the [adapter](crate::adapter) module.
2use std::{convert::Infallible, fmt};
3
4use serde::{Deserialize, Serialize};
5
6use crate::parser::ParserError;
7
8/// Error type when using the underlying engine.io socket
9#[derive(Debug, thiserror::Error, Serialize, Deserialize, Clone)]
10pub enum SocketError {
11    /// The socket channel is full.
12    /// You might need to increase the channel size with the [`SocketIoBuilder::max_buffer_size`] method.
13    ///
14    /// [`SocketIoBuilder::max_buffer_size`]: https://docs.rs/socketioxide/latest/socketioxide/struct.SocketIoBuilder.html#method.max_buffer_size
15    #[error("internal channel full error")]
16    InternalChannelFull,
17
18    /// The socket is already closed
19    #[error("socket closed")]
20    Closed,
21}
22
23/// Error type for the [`CoreAdapter`](crate::adapter::CoreAdapter) trait.
24#[derive(Debug, thiserror::Error)]
25pub struct AdapterError(#[from] pub Box<dyn std::error::Error + Send>);
26impl fmt::Display for AdapterError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        fmt::Display::fmt(&self.0, f)
29    }
30}
31impl From<Infallible> for AdapterError {
32    fn from(_: Infallible) -> Self {
33        panic!("Infallible should never be constructed, this is a bug")
34    }
35}
36
37/// Error type for broadcast operations.
38#[derive(thiserror::Error, Debug)]
39pub enum BroadcastError {
40    // This type should never constructed with an empty vector!
41    /// An error occurred while sending packets.
42    #[error("Error sending data through the engine.io socket: {0:?}")]
43    Socket(Vec<SocketError>),
44
45    /// An error occurred while serializing the packet.
46    #[error("Error serializing packet: {0:?}")]
47    Serialize(#[from] ParserError),
48
49    /// An error occured while broadcasting to other nodes.
50    #[error("Adapter error: {0}")]
51    Adapter(#[from] AdapterError),
52}
53
54impl From<Vec<SocketError>> for BroadcastError {
55    fn from(value: Vec<SocketError>) -> Self {
56        assert!(
57            !value.is_empty(),
58            "Cannot construct a BroadcastError from an empty vec of SocketError"
59        );
60        Self::Socket(value)
61    }
62}