use std::{error::Error, fmt::Write, time::Duration};
use simulator_api::{BacktestError, BacktestResponse};
use thiserror::Error;
use tokio_tungstenite::tungstenite;
pub type BacktestClientResult<T> = Result<T, BacktestClientError>;
pub(crate) fn err_chain(e: &(dyn Error + 'static)) -> String {
let mut out = e.to_string();
let mut src = e.source();
while let Some(s) = src {
let _ = write!(&mut out, ": {s}");
src = s.source();
}
out
}
#[derive(Debug, Error)]
#[allow(missing_docs)]
pub enum BacktestClientError {
#[error("invalid params: {message}")]
InvalidParams { message: String },
#[error("invalid API key header value: {0}")]
InvalidApiKeyHeader(#[from] tungstenite::http::header::InvalidHeaderValue),
#[error("{url} is not a usable endpoint: {source}")]
InvalidUrl {
url: String,
#[source]
source: http::uri::InvalidUri,
},
#[error("failed to build websocket request for {url}: {source}")]
BuildRequest {
url: String,
#[source]
source: Box<tungstenite::Error>,
},
#[error("websocket connect to {url} failed: {source}")]
Connect {
url: String,
#[source]
source: Box<tungstenite::Error>,
},
#[error("timeout while {action} after {duration:?}")]
Timeout {
action: &'static str,
duration: Duration,
},
#[error("failed to serialize request: {source}")]
SerializeRequest {
#[source]
source: serde_json::Error,
},
#[error("failed to deserialize response: {source}; raw={raw}")]
DeserializeResponse {
raw: String,
#[source]
source: serde_json::Error,
},
#[error("remote error: {0}")]
Remote(#[from] BacktestError),
#[error("websocket closed: {reason}")]
Closed {
reason: String,
},
#[error("unexpected response while {context}: {response:?}")]
UnexpectedResponse {
context: &'static str,
response: Box<BacktestResponse>,
},
#[error("websocket error while {action}: {source}")]
WebSocket {
action: &'static str,
#[source]
source: Box<tungstenite::Error>,
},
#[error("HTTP request to {url} failed: {source}")]
Http {
url: String,
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error("HTTP {status} from {url}: {body}")]
HttpStatus {
url: String,
status: reqwest::StatusCode,
body: String,
},
}
impl BacktestClientError {
pub fn is_retryable(&self) -> bool {
match self {
Self::Connect { source, .. } => match source.as_ref() {
tungstenite::Error::Http(response) => is_retryable_status(response.status()),
_ => true,
},
Self::Timeout { .. } | Self::Closed { .. } | Self::WebSocket { .. } => true,
Self::Remote(err) => err.is_retryable(),
Self::Http { source, .. } => source
.downcast_ref::<reqwest::Error>()
.is_some_and(|e| !e.is_decode() && !e.is_builder()),
Self::HttpStatus { status, .. } => is_retryable_status(*status),
Self::InvalidParams { .. }
| Self::InvalidApiKeyHeader(_)
| Self::InvalidUrl { .. }
| Self::BuildRequest { .. }
| Self::SerializeRequest { .. }
| Self::DeserializeResponse { .. }
| Self::UnexpectedResponse { .. } => false,
}
}
}
fn is_retryable_status(status: http::StatusCode) -> bool {
status == http::StatusCode::TOO_MANY_REQUESTS || status.is_server_error()
}