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