Skip to main content

socketio_next/
error.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use serde_json::Value;
5
6use crate::{engineio, protocol};
7
8/// Everything that can go wrong in the socket.io client.
9///
10/// Typed so callers never have to match on message text. Messages say what happened and what
11/// to check; they never contain query values (tokens) of the server URL.
12#[derive(Debug, Clone, thiserror::Error)]
13#[non_exhaustive]
14pub enum Error {
15    /// The server URL does not parse.
16    #[error("invalid server URL: {0}")]
17    InvalidUrl(#[from] url::ParseError),
18    /// The engine.io layer failed: connection refused, DNS, TLS, HTTP status, websocket,
19    /// handshake, ping timeout. [`engineio::Error`] has the details.
20    #[error(transparent)]
21    Engine(#[from] engineio::Error),
22    /// The server sent a packet that breaks the socket.io protocol.
23    #[error(transparent)]
24    Protocol(#[from] protocol::Error),
25    /// The connect attempt (engine.io handshake, upgrade and namespace CONNECT) took longer
26    /// than the builder's `timeout`.
27    #[error(
28        "connect attempt timed out after {0:?} (engine.io handshake + namespace CONNECT); check the URL, the path and that a socket.io v3+ server listens there"
29    )]
30    ConnectTimeout(Duration),
31    /// The server refused the namespace (CONNECT_ERROR): a middleware rejected the auth, or the
32    /// namespace does not exist. socket.io-client's `connect_error` with `err.message` /
33    /// `err.data`. The client does not reconnect after this.
34    #[error("the server rejected the namespace: {message}")]
35    #[non_exhaustive]
36    ConnectRejected {
37        /// The error message from the server middleware (`next(new Error(message))`).
38        message: String,
39        /// `err.data` from the server middleware, if any.
40        data: Option<Value>,
41    },
42    /// The server answered CONNECT without a session id: a Socket.IO v2 server, which this
43    /// v3+/v4 client cannot talk to.
44    #[error(
45        "the server answered CONNECT without a sid: it looks like a Socket.IO v2 server, which a v4 client cannot use"
46    )]
47    IncompatibleServer,
48    /// The auth payload is not a JSON object (socket.io requires one), or `auth_with` panicked.
49    #[error("invalid auth: {0}")]
50    InvalidAuth(&'static str),
51    /// `emit` while not connected, without `buffer_while_disconnected`.
52    #[error("not connected: the emit was dropped (enable buffer_while_disconnected to queue it)")]
53    NotConnected,
54    /// The client was closed (`disconnect()`, a rejection, reconnect attempts exhausted).
55    #[error("the client is closed")]
56    Closed,
57    /// No ack within the timeout given to `emit_with_ack`.
58    #[error("no ack from the server within {0:?}")]
59    AckTimeout(Duration),
60    /// The connection was lost while an ack was pending.
61    #[error("the connection was lost before the ack arrived")]
62    Disconnected,
63    /// The event name is reserved (`connect`, `connect_error`, `disconnect`, `disconnecting`,
64    /// `newListener`, `removeListener`): socket.io-client throws on these too.
65    #[error("`{0}` is a reserved event name")]
66    ReservedEvent(String),
67    /// A value could not be converted to or from JSON.
68    #[error("JSON conversion failed: {0}")]
69    Json(#[source] Arc<serde_json::Error>),
70    /// A header name or value is not valid HTTP.
71    #[error("invalid HTTP header: {0}")]
72    InvalidHeader(String),
73}
74
75impl From<serde_json::Error> for Error {
76    fn from(err: serde_json::Error) -> Self {
77        Error::Json(Arc::new(err))
78    }
79}
80
81/// Result alias of this crate.
82pub type Result<T> = std::result::Result<T, Error>;