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("Websocket closed with code {code}: {reason}")]
40    WebsocketClosed { code: u16, reason: String },
41    #[error("Got illegal handshake response: {0}")]
42    InvalidHandshake(String),
43    #[error("Called an action before the connection was established")]
44    IllegalActionBeforeOpen(),
45    #[error("Error setting up the http request: {0}")]
46    InvalidHttpConfiguration(#[from] http::Error),
47    #[error("string is not json serializable: {0}")]
48    InvalidJson(#[from] JsonError),
49    #[error("A lock was poisoned")]
50    InvalidPoisonedLock(),
51    #[error("Got an IO-Error: {0}")]
52    IncompleteIo(#[from] IoError),
53    #[error("Server did not allow upgrading to websockets")]
54    IllegalWebsocketUpgrade(),
55    #[error("Invalid header name")]
56    InvalidHeaderNameFromReqwest(#[from] reqwest::header::InvalidHeaderName),
57    #[error("Invalid header value")]
58    InvalidHeaderValueFromReqwest(#[from] reqwest::header::InvalidHeaderValue),
59    #[error("The server did not send a PING packet in time")]
60    PingTimeout(),
61}
62
63pub(crate) type Result<T> = std::result::Result<T, Error>;
64
65impl<T> From<std::sync::PoisonError<T>> for Error {
66    fn from(_: std::sync::PoisonError<T>) -> Self {
67        Self::InvalidPoisonedLock()
68    }
69}
70
71impl From<Error> for std::io::Error {
72    fn from(err: Error) -> std::io::Error {
73        std::io::Error::other(err)
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use std::sync::{Mutex, PoisonError};
80
81    use super::*;
82
83    /// This just tests the own implementations and relies on `thiserror` for the others.
84    #[test]
85    fn test_error_conversion() {
86        let mutex = Mutex::new(0);
87        let _error = Error::from(PoisonError::new(mutex.lock()));
88        assert!(matches!(Error::InvalidPoisonedLock(), _error));
89
90        let _io_error = std::io::Error::from(Error::IllegalWebsocketUpgrade());
91        let _error =
92            std::io::Error::new(std::io::ErrorKind::Other, Error::IllegalWebsocketUpgrade());
93        assert!(matches!(_io_error, _error));
94    }
95}