Skip to main content

simulator_client/
error.rs

1use std::time::Duration;
2
3use simulator_api::{BacktestError, BacktestResponse};
4use thiserror::Error;
5use tokio_tungstenite::tungstenite;
6
7pub type BacktestClientResult<T> = Result<T, BacktestClientError>;
8
9#[derive(Debug, Error)]
10pub enum BacktestClientError {
11    #[error("invalid params: {message}")]
12    InvalidParams { message: String },
13
14    #[error("invalid API key header value: {0}")]
15    InvalidApiKeyHeader(#[from] tungstenite::http::header::InvalidHeaderValue),
16
17    #[error("failed to build websocket request for {url}: {source}")]
18    BuildRequest {
19        url: String,
20        #[source]
21        source: Box<tungstenite::Error>,
22    },
23
24    #[error("websocket connect to {url} failed: {source}")]
25    Connect {
26        url: String,
27        #[source]
28        source: Box<tungstenite::Error>,
29    },
30
31    #[error("timeout while {action} after {duration:?}")]
32    Timeout {
33        action: &'static str,
34        duration: Duration,
35    },
36
37    #[error("failed to serialize request: {source}")]
38    SerializeRequest {
39        #[source]
40        source: serde_json::Error,
41    },
42
43    #[error("failed to deserialize response: {source}; raw={raw}")]
44    DeserializeResponse {
45        raw: String,
46        #[source]
47        source: serde_json::Error,
48    },
49
50    #[error("remote error: {0}")]
51    Remote(#[from] BacktestError),
52
53    #[error("websocket closed: {reason}")]
54    Closed { reason: String },
55
56    #[error("unexpected response while {context}: {response:?}")]
57    UnexpectedResponse {
58        context: &'static str,
59        response: Box<BacktestResponse>,
60    },
61
62    #[error("websocket error while {action}: {source}")]
63    WebSocket {
64        action: &'static str,
65        #[source]
66        source: Box<tungstenite::Error>,
67    },
68}