simulator_client/
error.rs1use std::{error::Error, fmt::Write, time::Duration};
2
3use simulator_api::{BacktestError, BacktestResponse};
4use thiserror::Error;
5use tokio_tungstenite::tungstenite;
6
7pub type BacktestClientResult<T> = Result<T, BacktestClientError>;
8
9pub(crate) fn err_chain(e: &(dyn Error + 'static)) -> String {
16 let mut out = e.to_string();
17 let mut src = e.source();
18 while let Some(s) = src {
19 let _ = write!(&mut out, ": {s}");
20 src = s.source();
21 }
22 out
23}
24
25#[derive(Debug, Error)]
26pub enum BacktestClientError {
27 #[error("invalid params: {message}")]
28 InvalidParams { message: String },
29
30 #[error("invalid API key header value: {0}")]
31 InvalidApiKeyHeader(#[from] tungstenite::http::header::InvalidHeaderValue),
32
33 #[error("{url} is not a usable endpoint: {source}")]
34 InvalidUrl {
35 url: String,
36 #[source]
37 source: http::uri::InvalidUri,
38 },
39
40 #[error("failed to build websocket request for {url}: {source}")]
41 BuildRequest {
42 url: String,
43 #[source]
44 source: Box<tungstenite::Error>,
45 },
46
47 #[error("websocket connect to {url} failed: {source}")]
48 Connect {
49 url: String,
50 #[source]
51 source: Box<tungstenite::Error>,
52 },
53
54 #[error("timeout while {action} after {duration:?}")]
55 Timeout {
56 action: &'static str,
57 duration: Duration,
58 },
59
60 #[error("failed to serialize request: {source}")]
61 SerializeRequest {
62 #[source]
63 source: serde_json::Error,
64 },
65
66 #[error("failed to deserialize response: {source}; raw={raw}")]
67 DeserializeResponse {
68 raw: String,
69 #[source]
70 source: serde_json::Error,
71 },
72
73 #[error("remote error: {0}")]
74 Remote(#[from] BacktestError),
75
76 #[error("websocket closed: {reason}")]
77 Closed { reason: String },
78
79 #[error("unexpected response while {context}: {response:?}")]
80 UnexpectedResponse {
81 context: &'static str,
82 response: Box<BacktestResponse>,
83 },
84
85 #[error("websocket error while {action}: {source}")]
86 WebSocket {
87 action: &'static str,
88 #[source]
89 source: Box<tungstenite::Error>,
90 },
91
92 #[error("HTTP request to {url} failed: {source}")]
93 Http {
94 url: String,
95 #[source]
96 source: Box<dyn std::error::Error + Send + Sync>,
97 },
98
99 #[error("HTTP {status} from {url}: {body}")]
100 HttpStatus {
101 url: String,
102 status: reqwest::StatusCode,
103 body: String,
104 },
105}