kraken_async_rs/wss/
errors.rs1pub use serde_json::Error as SerdeError;
4use std::error::Error;
5use std::fmt::{Display, Formatter};
6pub use tokio_tungstenite::tungstenite::Error as TungsteniteError;
7pub use url::ParseError as UrlParseError;
8
9#[derive(Debug)]
11pub enum WSSError {
12 Serde(SerdeError),
13 WSS(TungsteniteError),
14 UrlParse(UrlParseError),
15}
16
17impl From<SerdeError> for WSSError {
18 fn from(value: SerdeError) -> Self {
19 Self::Serde(value)
20 }
21}
22
23impl From<TungsteniteError> for WSSError {
24 fn from(value: TungsteniteError) -> Self {
25 Self::WSS(value)
26 }
27}
28
29impl From<UrlParseError> for WSSError {
30 fn from(value: UrlParseError) -> Self {
31 Self::UrlParse(value)
32 }
33}
34
35impl Display for WSSError {
36 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
37 match self {
38 WSSError::Serde(err) => write!(f, "{err}"),
39 WSSError::WSS(err) => write!(f, "{err}"),
40 WSSError::UrlParse(err) => write!(f, "{err}"),
41 }
42 }
43}
44
45impl Error for WSSError {
46 fn source(&self) -> Option<&(dyn Error + 'static)> {
47 match self {
48 WSSError::Serde(e) => Some(e),
49 WSSError::WSS(e) => Some(e),
50 WSSError::UrlParse(e) => Some(e),
51 }
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use crate::wss::errors::WSSError;
58 use serde::de::Error as DeError;
59 use serde_json::Error as SerdeError;
60 use tokio_tungstenite::tungstenite::Error as TungsteniteError;
61 use url::ParseError as UrlParseError;
62
63 #[test]
64 fn test_error_conversion() {
65 let serde_error = SerdeError::custom("id");
66 let tungstenite_error = TungsteniteError::ConnectionClosed;
67 let url_parse_error = UrlParseError::InvalidPort;
68
69 let error = WSSError::from(serde_error);
70 assert!(matches!(error, WSSError::Serde { .. }));
71
72 let error = WSSError::from(tungstenite_error);
73 assert!(matches!(error, WSSError::WSS { .. }));
74
75 let error = WSSError::from(url_parse_error);
76 assert!(matches!(error, WSSError::UrlParse { .. }));
77 }
78}