Skip to main content

socketio_next/protocol/
error.rs

1use std::str::Utf8Error;
2use std::sync::Arc;
3
4/// A packet that does not follow the Engine.IO v4 / Socket.IO v5 wire format.
5#[derive(Debug, Clone, thiserror::Error)]
6#[non_exhaustive]
7pub enum Error {
8    /// The first character is not a known packet type.
9    #[error("unknown packet type {0:?}")]
10    InvalidPacketType(char),
11    /// The packet ends before a required part (type, attachment count, namespace).
12    #[error("incomplete packet")]
13    IncompletePacket,
14    /// The packet breaks a protocol rule; the text says which.
15    #[error("invalid packet: {0}")]
16    InvalidPacket(&'static str),
17    /// A text packet is not valid UTF-8.
18    #[error("packet is not valid UTF-8: {0}")]
19    InvalidUtf8(#[from] Utf8Error),
20    /// A `b`-prefixed (base64) engine.io packet does not decode.
21    #[error("invalid base64 in a binary packet: {0}")]
22    InvalidBase64(#[from] base64::DecodeError),
23    /// The JSON payload does not parse.
24    #[error("invalid JSON payload: {0}")]
25    InvalidJson(#[source] Arc<serde_json::Error>),
26}
27
28impl From<serde_json::Error> for Error {
29    fn from(err: serde_json::Error) -> Self {
30        Error::InvalidJson(Arc::new(err))
31    }
32}
33
34/// Result alias of this crate.
35pub type Result<T> = std::result::Result<T, Error>;