socketio_rs/
error.rs

1use base64::DecodeError;
2use serde_json::Error as JsonError;
3use std::io::Error as IoError;
4use std::num::ParseIntError;
5use std::str::Utf8Error;
6use thiserror::Error;
7use url::ParseError as UrlParseError;
8
9/// Enumeration of all possible errors in the `socket.io` context.
10#[derive(Error, Debug)]
11#[non_exhaustive]
12pub enum Error {
13    // Conform to https://rust-lang.github.io/api-guidelines/naming.html#names-use-a-consistent-word-order-c-word-order
14    // Negative verb-object
15    #[error("Invalid packet id: {0}")]
16    InvalidPacketType(char),
17    #[error("Error while parsing an incomplete packet")]
18    IncompletePacket(),
19    #[error("Got an invalid packet which did not follow the protocol format")]
20    InvalidPacket(),
21    #[error("An error occurred while decoding the utf-8 text: {0}")]
22    InvalidUtf8(#[from] Utf8Error),
23    #[error("An error occurred while encoding/decoding base64: {0}")]
24    InvalidBase64(#[from] DecodeError),
25    #[error("Invalid Url during parsing")]
26    InvalidUrl(#[from] UrlParseError),
27    #[error("Invalid Url Scheme: {0}")]
28    InvalidUrlScheme(String),
29    #[error("Got illegal handshake response: {0}")]
30    InvalidHandshake(String),
31    #[error("Called an action before the connection was established")]
32    IllegalActionBeforeOpen(),
33    #[error("string is not json serializable: {0}")]
34    InvalidJson(#[from] JsonError),
35    #[error("A lock was poisoned")]
36    InvalidPoisonedLock(),
37    #[error("Got an IO-Error: {0}")]
38    IncompleteIo(#[from] IoError),
39    #[error("Error while parsing an integer")]
40    InvalidInteger(#[from] ParseIntError),
41    #[error("EngineIO Error")]
42    IncompleteResponseFromEngineIo(#[from] engineio_rs::Error),
43    #[error("Invalid packet type while reading attachments")]
44    InvalidAttachmentPacketType(u8),
45    #[error("Invalid reconnect: {0}")]
46    InvalidReconnect(String),
47    #[error("Underlying Engine.IO connection has closed")]
48    StoppedEngineIoSocket,
49}
50
51pub type Result<T> = std::result::Result<T, Error>;
52
53impl<T> From<std::sync::PoisonError<T>> for Error {
54    fn from(_: std::sync::PoisonError<T>) -> Self {
55        Self::InvalidPoisonedLock()
56    }
57}
58
59impl From<Error> for std::io::Error {
60    fn from(err: Error) -> std::io::Error {
61        std::io::Error::new(std::io::ErrorKind::Other, err)
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use std::sync::{Mutex, PoisonError};
68
69    use super::*;
70
71    /// This just tests the own implementations and relies on `thiserror` for the others.
72    #[test]
73    fn test_error_conversion() {
74        let mutex = Mutex::new(0);
75        let _error = Error::from(PoisonError::new(mutex.lock()));
76        assert!(matches!(Error::InvalidPoisonedLock(), _error));
77
78        let _io_error = std::io::Error::from(Error::IncompletePacket());
79        let _error = std::io::Error::new(std::io::ErrorKind::Other, Error::IncompletePacket());
80        assert!(matches!(_io_error, _error));
81    }
82}