Skip to main content

tf_rust_socketio/
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/// TODO: 0.4.X Do not expose non-trivial internal errors. Convert error to string.
11#[derive(Error, Debug)]
12#[non_exhaustive]
13#[cfg_attr(tarpaulin, ignore)]
14pub enum Error {
15    // Conform to https://rust-lang.github.io/api-guidelines/naming.html#names-use-a-consistent-word-order-c-word-order
16    // Negative verb-object
17    #[error("Invalid packet id: {0}")]
18    InvalidPacketId(char),
19    #[error("Error while parsing an incomplete packet")]
20    IncompletePacket(),
21    #[error("Got an invalid packet which did not follow the protocol format")]
22    InvalidPacket(),
23    #[error("An error occurred while decoding the utf-8 text: {0}")]
24    InvalidUtf8(#[from] Utf8Error),
25    #[error("An error occurred while encoding/decoding base64: {0}")]
26    InvalidBase64(#[from] DecodeError),
27    #[error("Invalid Url during parsing")]
28    InvalidUrl(#[from] UrlParseError),
29    #[error("Invalid Url Scheme: {0}")]
30    InvalidUrlScheme(String),
31    #[error("Got illegal handshake response: {0}")]
32    InvalidHandshake(String),
33    #[error("Called an action before the connection was established")]
34    IllegalActionBeforeOpen(),
35    #[error("string is not json serializable: {0}")]
36    InvalidJson(#[from] JsonError),
37    #[error("A lock was poisoned")]
38    InvalidPoisonedLock(),
39    #[error("Got an IO-Error: {0}")]
40    IncompleteIo(#[from] IoError),
41    #[error("Error while parsing an integer")]
42    InvalidInteger(#[from] ParseIntError),
43    #[error("EngineIO Error")]
44    IncompleteResponseFromEngineIo(#[from] tf_rust_engineio::Error),
45    #[error("Invalid packet type while reading attachments")]
46    InvalidAttachmentPacketType(u8),
47    #[error("Underlying Engine.IO connection has closed")]
48    StoppedEngineIoSocket,
49}
50
51pub(crate) 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::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}