Skip to main content

rustrade_integration/
error.rs

1use crate::subscription::{SubscriptionId, display_subscription_ids};
2use prost::DecodeError;
3use reqwest::Error;
4use thiserror::Error;
5
6/// All socket IO related errors generated in `rustrade-integration`.
7#[derive(Debug, Error)]
8pub enum SocketError {
9    #[error("Sink error")]
10    Sink,
11
12    #[error("Deserialising JSON error: {error} for payload: {payload}")]
13    Deserialise {
14        error: serde_json::Error,
15        payload: String,
16    },
17
18    #[error("Deserialising JSON error: {error} for binary payload: {payload:?}")]
19    DeserialiseBinary {
20        error: serde_json::Error,
21        payload: Vec<u8>,
22    },
23
24    #[error("Deserialising protobuf error: {error} for binary payload: {payload:?}")]
25    DeserialiseProtobuf {
26        error: DecodeError,
27        payload: Vec<u8>,
28    },
29
30    #[error("Serialising JSON error: {0}")]
31    Serialise(serde_json::Error),
32
33    #[error("SerDe Query String serialisation error: {0}")]
34    QueryParams(#[from] serde_qs::Error),
35
36    #[error("SerDe url encoding serialisation error: {0}")]
37    UrlEncoded(#[from] serde_urlencoded::ser::Error),
38
39    #[error("error parsing Url: {0}")]
40    UrlParse(#[from] url::ParseError),
41
42    #[error("error subscribing to resources over the socket: {0}")]
43    Subscribe(String),
44
45    /// Subscription validation ended before every subscription in a batch was acknowledged.
46    ///
47    /// `unacknowledged` holds the subscriptions that received no acknowledgement, sorted. They are
48    /// candidates, not proof of rejection: a venue that closes the connection on one bad
49    /// subscription also leaves those sent after it unanswered. When the venue's acknowledgements
50    /// cannot be matched to subscriptions, it holds the whole batch.
51    #[error(
52        "subscriptions not acknowledged ({reason}): {}",
53        display_subscription_ids(unacknowledged)
54    )]
55    Unacknowledged {
56        reason: String,
57        unacknowledged: Vec<SubscriptionId>,
58    },
59
60    #[error("ExchangeStream terminated with closing frame: {0}")]
61    Terminated(String),
62
63    #[error("{entity} does not support: {item}")]
64    Unsupported { entity: String, item: String },
65
66    #[error("WebSocket error: {0}")]
67    WebSocket(Box<tokio_tungstenite::tungstenite::Error>),
68
69    #[error("HTTP error: {0}")]
70    Http(reqwest::Error),
71
72    #[error("HTTP request timed out")]
73    HttpTimeout(reqwest::Error),
74
75    /// REST http response error
76    #[error("HTTP response (status={0}) error: {1}")]
77    HttpResponse(reqwest::StatusCode, String),
78
79    #[error("consumed unidentifiable message: {0}")]
80    Unidentifiable(SubscriptionId),
81
82    #[error("consumed error message from execution: {0}")]
83    Exchange(String),
84}
85
86impl From<reqwest::Error> for SocketError {
87    fn from(error: Error) -> Self {
88        match error {
89            error if error.is_timeout() => SocketError::HttpTimeout(error),
90            error => SocketError::Http(error),
91        }
92    }
93}