kraken_async_rs/wss/
errors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! Top level errors produced by [KrakenWSSClient] and [KrakenMessageStream]
//!
#[allow(unused)]
use crate::wss::kraken_wss_client::KrakenMessageStream;
#[allow(unused)]
#[allow(deprecated)]
use crate::wss::kraken_wss_client::KrakenWSSClient;
pub use serde_json::Error as SerdeError;
use std::error::Error;
use std::fmt::{Display, Formatter};
pub use tokio_tungstenite::tungstenite::Error as TungsteniteError;
pub use url::ParseError as UrlParseError;

/// Aggregate error type produced by [KrakenWSSClient] and [KrakenMessageStream]
#[derive(Debug)]
pub enum WSSError {
    Serde(SerdeError),
    WSS(TungsteniteError),
    UrlParse(UrlParseError),
}

impl From<SerdeError> for WSSError {
    fn from(value: SerdeError) -> Self {
        Self::Serde(value)
    }
}

impl From<TungsteniteError> for WSSError {
    fn from(value: TungsteniteError) -> Self {
        Self::WSS(value)
    }
}

impl From<UrlParseError> for WSSError {
    fn from(value: UrlParseError) -> Self {
        Self::UrlParse(value)
    }
}

impl Display for WSSError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            WSSError::Serde(err) => write!(f, "{err}"),
            WSSError::WSS(err) => write!(f, "{err}"),
            WSSError::UrlParse(err) => write!(f, "{err}"),
        }
    }
}

impl Error for WSSError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            WSSError::Serde(e) => Some(e),
            WSSError::WSS(e) => Some(e),
            WSSError::UrlParse(e) => Some(e),
        }
    }
}