1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use base64::DecodeError;
use serde_json::Error as JsonError;
use std::io::Error as IoError;
use std::num::ParseIntError;
use std::str::Utf8Error;
use thiserror::Error;
use url::ParseError as UrlParseError;

/// Enumeration of all possible errors in the `socket.io` context.
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
    // Conform to https://rust-lang.github.io/api-guidelines/naming.html#names-use-a-consistent-word-order-c-word-order
    // Negative verb-object
    #[error("Invalid packet id: {0}")]
    InvalidPacketType(char),
    #[error("Error while parsing an incomplete packet")]
    IncompletePacket(),
    #[error("Got an invalid packet which did not follow the protocol format")]
    InvalidPacket(),
    #[error("An error occurred while decoding the utf-8 text: {0}")]
    InvalidUtf8(#[from] Utf8Error),
    #[error("An error occurred while encoding/decoding base64: {0}")]
    InvalidBase64(#[from] DecodeError),
    #[error("Invalid Url during parsing")]
    InvalidUrl(#[from] UrlParseError),
    #[error("Invalid Url Scheme: {0}")]
    InvalidUrlScheme(String),
    #[error("Got illegal handshake response: {0}")]
    InvalidHandshake(String),
    #[error("Called an action before the connection was established")]
    IllegalActionBeforeOpen(),
    #[error("string is not json serializable: {0}")]
    InvalidJson(#[from] JsonError),
    #[error("A lock was poisoned")]
    InvalidPoisonedLock(),
    #[error("Got an IO-Error: {0}")]
    IncompleteIo(#[from] IoError),
    #[error("Error while parsing an integer")]
    InvalidInteger(#[from] ParseIntError),
    #[error("EngineIO Error")]
    IncompleteResponseFromEngineIo(#[from] engineio_rs::Error),
    #[error("Invalid packet type while reading attachments")]
    InvalidAttachmentPacketType(u8),
    #[error("Invalid reconnect: {0}")]
    InvalidReconnect(String),
    #[error("Underlying Engine.IO connection has closed")]
    StoppedEngineIoSocket,
}

pub type Result<T> = std::result::Result<T, Error>;

impl<T> From<std::sync::PoisonError<T>> for Error {
    fn from(_: std::sync::PoisonError<T>) -> Self {
        Self::InvalidPoisonedLock()
    }
}

impl From<Error> for std::io::Error {
    fn from(err: Error) -> std::io::Error {
        std::io::Error::new(std::io::ErrorKind::Other, err)
    }
}

#[cfg(test)]
mod tests {
    use std::sync::{Mutex, PoisonError};

    use super::*;

    /// This just tests the own implementations and relies on `thiserror` for the others.
    #[test]
    fn test_error_conversion() {
        let mutex = Mutex::new(0);
        let _error = Error::from(PoisonError::new(mutex.lock()));
        assert!(matches!(Error::InvalidPoisonedLock(), _error));

        let _io_error = std::io::Error::from(Error::IncompletePacket());
        let _error = std::io::Error::new(std::io::ErrorKind::Other, Error::IncompletePacket());
        assert!(matches!(_io_error, _error));
    }
}