Skip to main content

tf_rust_engineio/
error.rs

1use base64::DecodeError;
2use reqwest::Error as ReqwestError;
3use serde_json::Error as JsonError;
4use std::io::Error as IoError;
5use std::str::Utf8Error;
6use thiserror::Error;
7use tungstenite::Error as TungsteniteError;
8use url::ParseError as UrlParseError;
9
10/// Enumeration of all possible errors in the `socket.io` context.
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(u8),
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("Error during connection via http: {0}")]
32    IncompleteResponseFromReqwest(#[from] ReqwestError),
33    #[error("Error with websocket connection: {0}")]
34    WebsocketError(#[from] TungsteniteError),
35    #[error("Network request returned with status code: {0}")]
36    IncompleteHttp(u16),
37    #[error("Network request returned with status code {status}, body: {body}")]
38    HttpErrorWithBody { status: u16, body: String },
39    #[error("Got illegal handshake response: {0}")]
40    InvalidHandshake(String),
41    #[error("Called an action before the connection was established")]
42    IllegalActionBeforeOpen(),
43    #[error("Error setting up the http request: {0}")]
44    InvalidHttpConfiguration(#[from] http::Error),
45    #[error("string is not json serializable: {0}")]
46    InvalidJson(#[from] JsonError),
47    #[error("A lock was poisoned")]
48    InvalidPoisonedLock(),
49    #[error("Got an IO-Error: {0}")]
50    IncompleteIo(#[from] IoError),
51    #[error("Server did not allow upgrading to websockets")]
52    IllegalWebsocketUpgrade(),
53    #[error("Invalid header name")]
54    InvalidHeaderNameFromReqwest(#[from] reqwest::header::InvalidHeaderName),
55    #[error("Invalid header value")]
56    InvalidHeaderValueFromReqwest(#[from] reqwest::header::InvalidHeaderValue),
57    #[error("The server did not send a PING packet in time")]
58    PingTimeout(),
59}
60
61pub(crate) type Result<T> = std::result::Result<T, Error>;
62
63impl<T> From<std::sync::PoisonError<T>> for Error {
64    fn from(_: std::sync::PoisonError<T>) -> Self {
65        Self::InvalidPoisonedLock()
66    }
67}
68
69impl From<Error> for std::io::Error {
70    fn from(err: Error) -> std::io::Error {
71        std::io::Error::other(err)
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use std::sync::{Mutex, PoisonError};
78
79    use super::*;
80
81    /// This just tests the own implementations and relies on `thiserror` for the others.
82    #[test]
83    fn test_error_conversion() {
84        let mutex = Mutex::new(0);
85        let _error = Error::from(PoisonError::new(mutex.lock()));
86        assert!(matches!(Error::InvalidPoisonedLock(), _error));
87
88        let _io_error = std::io::Error::from(Error::IllegalWebsocketUpgrade());
89        let _error =
90            std::io::Error::new(std::io::ErrorKind::Other, Error::IllegalWebsocketUpgrade());
91        assert!(matches!(_io_error, _error));
92    }
93}