Skip to main content

rustrade_execution/
error.rs

1//! Error types for [`ExecutionClient`](super::client::ExecutionClient) operations.
2//!
3//! # Retry Semantics
4//!
5//! Use [`ClientError::is_transient`] to determine if an operation should be retried.
6//! Transient errors (connectivity issues, rate limits) may succeed on retry with
7//! appropriate backoff. Non-transient errors (invalid instrument, insufficient
8//! balance) will fail identically on retry — the caller must change the request.
9//!
10//! The `is_transient()` method is the stable contract for retry decisions. Prefer
11//! it over pattern matching on specific variants, as the internal taxonomy may
12//! evolve while `is_transient()` semantics remain stable.
13
14use rustrade_instrument::{
15    asset::{AssetIndex, name::AssetNameExchange},
16    exchange::ExchangeId,
17    instrument::{InstrumentIndex, name::InstrumentNameExchange},
18};
19use rustrade_integration::error::SocketError;
20use serde::{Deserialize, Serialize};
21use thiserror::Error;
22
23/// Type alias for a [`ClientError`] that is keyed on [`AssetNameExchange`] and
24/// [`InstrumentNameExchange`] (yet to be indexed).
25pub type UnindexedClientError = ClientError<AssetNameExchange, InstrumentNameExchange>;
26
27/// Type alias for a [`ApiError`] that is keyed on [`AssetNameExchange`] and
28/// [`InstrumentNameExchange`] (yet to be indexed).
29pub type UnindexedApiError = ApiError<AssetNameExchange, InstrumentNameExchange>;
30
31/// Type alias for a [`OrderError`] that is keyed on [`AssetNameExchange`] and
32/// [`InstrumentNameExchange`] (yet to be indexed).
33pub type UnindexedOrderError = OrderError<AssetNameExchange, InstrumentNameExchange>;
34
35/// Represents all errors produced by an [`ExecutionClient`](super::client::ExecutionClient).
36#[non_exhaustive]
37#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Error)]
38pub enum ClientError<AssetKey = AssetIndex, InstrumentKey = InstrumentIndex> {
39    /// Connectivity based error.
40    ///
41    /// eg/ Timeout.
42    #[error("Connectivity: {0}")]
43    Connectivity(#[from] ConnectivityError),
44
45    /// API based error.
46    ///
47    /// eg/ RateLimit.
48    #[error("API: {0}")]
49    Api(#[from] ApiError<AssetKey, InstrumentKey>),
50
51    /// A background task panicked or was cancelled during an operation.
52    ///
53    /// This indicates a bug or unexpected runtime condition (e.g., a tokio
54    /// `spawn_blocking` task panicked). The operation was not retried and
55    /// the caller should treat this as non-recoverable, requiring operator
56    /// attention.
57    #[error("task failed: {0}")]
58    TaskFailed(String),
59
60    /// An opaque error from an upstream library that cannot be further classified.
61    ///
62    /// This is a catch-all for errors that don't fit into [`Self::Connectivity`] or
63    /// [`Self::Api`] categories — typically because the upstream library (e.g., ibapi,
64    /// binance-sdk) returns unstructured errors.
65    ///
66    /// Conservatively treated as non-transient. If you encounter this error
67    /// frequently, consider filing an issue to improve error classification.
68    #[error("internal error: {0}")]
69    Internal(String),
70
71    /// Activity pagination was truncated at the page limit.
72    ///
73    /// The returned data from the underlying call is a partial result. This error
74    /// indicates that more activities exist beyond the safety limit, typically due
75    /// to a very long outage (>5000 fills). Callers should alert operators and
76    /// consider manual reconciliation.
77    #[error("activity pagination truncated at {limit} pages — data may be incomplete")]
78    Truncated {
79        /// Maximum number of pages that were fetched before truncation.
80        limit: usize,
81    },
82
83    /// Open orders snapshot was truncated at the API's row limit.
84    ///
85    /// Unlike [`Self::Truncated`] (which applies to paginated activity fetches), this
86    /// error indicates a single-request endpoint hit its maximum row count.
87    /// Alpaca's `/v2/orders` endpoint caps results at 500; accounts with more
88    /// concurrent open orders will have an incomplete snapshot.
89    ///
90    /// Callers should alert operators — an incomplete order snapshot can cause
91    /// duplicate submissions, missed cancellations, or incorrect position sizing.
92    #[error("open orders snapshot truncated at {limit} results — data may be incomplete")]
93    TruncatedSnapshot {
94        /// Maximum number of rows returned by the single-request endpoint.
95        limit: usize,
96    },
97}
98
99impl<AssetKey, InstrumentKey> ClientError<AssetKey, InstrumentKey> {
100    /// Returns `true` if this error is likely transient and the operation
101    /// may succeed if retried after a suitable backoff.
102    ///
103    /// The caller is responsible for retry limits and backoff strategy.
104    /// This method classifies the error only — it does not implement policy.
105    ///
106    /// # Transient errors
107    /// - [`Connectivity`](Self::Connectivity) errors (timeout, socket, offline)
108    /// - [`Api::RateLimit`](ApiError::RateLimit)
109    ///
110    /// # Non-transient errors
111    /// - Other [`Api`](Self::Api) errors (invalid instrument, insufficient balance, etc.)
112    /// - [`TaskFailed`](Self::TaskFailed) (indicates a bug)
113    /// - [`Internal`](Self::Internal) (unknown — conservatively non-transient)
114    /// - [`Truncated`](Self::Truncated) / [`TruncatedSnapshot`](Self::TruncatedSnapshot)
115    pub fn is_transient(&self) -> bool {
116        match self {
117            Self::Connectivity(e) => e.is_transient(),
118            Self::Api(ApiError::RateLimit) => true,
119            Self::Api(_) => false,
120            Self::TaskFailed(_) => false,
121            Self::Internal(_) => false,
122            Self::Truncated { .. } => false,
123            Self::TruncatedSnapshot { .. } => false,
124        }
125    }
126}
127
128/// Represents all connectivity-centric errors.
129///
130/// Connectivity errors are generally intermittent / non-deterministic (eg/ Timeout).
131/// All variants are transient — retry with exponential backoff (typically 1-30s).
132#[non_exhaustive]
133#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Error)]
134pub enum ConnectivityError {
135    /// Exchange is offline, likely due to scheduled maintenance.
136    ///
137    /// Transient — retry with backoff. Maintenance windows typically last minutes
138    /// to hours; consider longer backoff intervals (30s-5min) to avoid log spam.
139    #[error("Exchange offline: {0}")]
140    ExchangeOffline(ExchangeId),
141
142    /// Request timed out before a response was received.
143    ///
144    /// Transient — retry with backoff. May indicate network congestion, server
145    /// overload, or an overly aggressive timeout. Consider increasing timeout
146    /// on subsequent attempts.
147    #[error("ExecutionRequest timed out")]
148    Timeout,
149
150    /// Network-level socket error (connection refused, reset, DNS failure, etc.).
151    ///
152    /// Transient — retry with backoff. If persistent, may indicate firewall
153    /// issues, incorrect endpoint configuration, or prolonged server outage.
154    #[error("{0}")]
155    Socket(String),
156}
157
158impl From<SocketError> for ConnectivityError {
159    fn from(value: SocketError) -> Self {
160        Self::Socket(value.to_string())
161    }
162}
163
164impl ConnectivityError {
165    /// Returns `true` if this connectivity error is transient.
166    ///
167    /// All connectivity errors are considered transient — they represent
168    /// temporary network or server conditions that may resolve with retry.
169    pub fn is_transient(&self) -> bool {
170        match self {
171            Self::ExchangeOffline(_) => true,
172            Self::Timeout => true,
173            Self::Socket(_) => true,
174        }
175    }
176}
177
178/// Represents all API errors generated by an exchange.
179///
180/// These typically indicate a request is invalid for some reason (eg/ BalanceInsufficient).
181/// Most variants are **not transient** — the same request will fail identically on retry.
182/// The exception is [`RateLimit`](Self::RateLimit), which is transient.
183#[non_exhaustive]
184#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Error)]
185pub enum ApiError<AssetKey = AssetIndex, InstrumentKey = InstrumentIndex> {
186    /// Provided asset identifier is invalid or not supported.
187    ///
188    /// For example:
189    /// - The [`AssetNameExchange`] was an invalid format.
190    ///
191    /// Not transient — do not retry. The asset identifier must be corrected.
192    #[error("asset {0} invalid: {1}")]
193    AssetInvalid(AssetKey, String),
194
195    /// Provided instrument identifier is invalid or not supported.
196    ///
197    /// For example:
198    /// - The exchange does not have a market for an instrument.
199    /// - The [`InstrumentNameExchange`] was an invalid format.
200    ///
201    /// Not transient — do not retry. The instrument identifier must be corrected.
202    #[error("instrument {0} invalid: {1}")]
203    InstrumentInvalid(InstrumentKey, String),
204
205    /// Request was rejected due to rate limiting.
206    ///
207    /// The exchange enforces request quotas and the caller has exceeded them.
208    /// Some exchanges provide a `Retry-After` header or similar hint; the client
209    /// may incorporate this into internal retry logic before surfacing this error.
210    ///
211    /// Transient — retry with backoff. Typical backoff is 10-60 seconds, but
212    /// respect exchange-specific guidance if available.
213    #[error("rate limit exceeded")]
214    RateLimit,
215
216    /// Authentication failed (invalid credentials, expired key, bad signature).
217    ///
218    /// Unlike other API errors which affect a single request, authentication
219    /// failures indicate that **all** subsequent requests will fail until
220    /// credentials are corrected. Callers should halt trading and alert operators.
221    ///
222    /// Not transient — do not retry. Fix credentials and restart.
223    #[error("authentication failed: {0}")]
224    Unauthenticated(String),
225
226    /// Balance of an asset is insufficient to execute the requested operation.
227    ///
228    /// # Warning: `AssetKey` field may hold an instrument name, not an asset name
229    ///
230    /// Some `ExecutionClient` implementations (e.g. `BinanceSpot`) populate the
231    /// `AssetKey` field with the **instrument name** (e.g. `"BTCUSDT"`) rather than
232    /// the specific low-balance asset (e.g. `"BTC"` or `"USDT"`), because splitting
233    /// a symbol into base/quote requires exchange symbol-info metadata not available
234    /// at error-parse time. Do **not** pattern-match on the `AssetKey` value to
235    /// identify the specific low-balance asset — use the `String` field for
236    /// diagnostics only.
237    ///
238    /// Not transient — do not retry the same request. Reduce order size or
239    /// deposit additional funds.
240    #[error("asset {0} balance insufficient: {1}")]
241    BalanceInsufficient(AssetKey, String),
242
243    /// Order was rejected by the exchange for a business rule violation.
244    ///
245    /// Common causes include: price outside allowed range, quantity below
246    /// minimum, post-only order would cross, reduce-only with no position.
247    ///
248    /// Not transient — do not retry the same request. Adjust order parameters.
249    #[error("order rejected: {0}")]
250    OrderRejected(String),
251
252    /// Cancel request failed because the order was already cancelled.
253    ///
254    /// This is a state conflict, not an error per se — the desired end state
255    /// (order cancelled) has already been achieved.
256    ///
257    /// Not transient — do not retry. The order is already in the cancelled state.
258    #[error("order already cancelled")]
259    OrderAlreadyCancelled,
260
261    /// Cancel request failed because the order was already fully filled.
262    ///
263    /// This is a state conflict — the order completed before the cancel arrived.
264    /// The caller should reconcile their local state with the fill.
265    ///
266    /// Not transient — do not retry. The order no longer exists to cancel.
267    #[error("order already fully filled")]
268    OrderAlreadyFullyFilled,
269}
270
271/// Represents all errors that can be generated when cancelling or opening orders.
272///
273/// This is a subset of [`ClientError`] for order-specific operations. Use
274/// [`is_transient()`](Self::is_transient) to determine retry eligibility.
275#[non_exhaustive]
276#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Error)]
277pub enum OrderError<AssetKey = AssetIndex, InstrumentKey = InstrumentIndex> {
278    /// Connectivity-based error (timeout, socket failure, exchange offline).
279    ///
280    /// Transient — retry with backoff. See [`ConnectivityError`] for details.
281    #[error("connectivity: {0}")]
282    Connectivity(#[from] ConnectivityError),
283
284    /// API-based error (rate limit, invalid instrument, order rejected, etc.).
285    ///
286    /// Retry semantics depend on the specific [`ApiError`] variant. Only
287    /// [`ApiError::RateLimit`] is transient; other variants are not.
288    #[error("order rejected: {0}")]
289    Rejected(#[from] ApiError<AssetKey, InstrumentKey>),
290
291    /// The order type is not supported by this connector.
292    ///
293    /// Non-transient — the connector does not support this order type (e.g.,
294    /// trailing stop orders on a connector that only supports market/limit).
295    #[error("unsupported order type: {0}")]
296    UnsupportedOrderType(String),
297}
298
299impl<AssetKey, InstrumentKey> OrderError<AssetKey, InstrumentKey> {
300    /// Returns `true` if this error is likely transient and the operation
301    /// may succeed if retried after a suitable backoff.
302    ///
303    /// # Transient errors
304    /// - [`Connectivity`](Self::Connectivity) errors (timeout, socket, offline)
305    /// - [`Rejected(ApiError::RateLimit)`](ApiError::RateLimit)
306    ///
307    /// # Non-transient errors
308    /// - Other [`Rejected`](Self::Rejected) errors (invalid instrument, insufficient balance, etc.)
309    pub fn is_transient(&self) -> bool {
310        match self {
311            Self::Connectivity(e) => e.is_transient(),
312            Self::Rejected(ApiError::RateLimit) => true,
313            Self::Rejected(_) => false,
314            Self::UnsupportedOrderType(_) => false,
315        }
316    }
317}
318
319/// Represents errors related to exchange, asset and instrument identifier key lookups.
320#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize, Error)]
321pub enum KeyError {
322    /// Indicates an [`ExchangeId`] was encountered that was not indexed, so does not have a
323    /// corresponding `ExchangeIndex`.
324    #[error("ExchangeId: {0}")]
325    ExchangeId(String),
326
327    /// Indicates an [`AssetNameExchange`] was encountered that was not indexed, so does not have a
328    /// corresponding [`AssetIndex`].
329    #[error("AssetKey: {0}")]
330    AssetKey(String),
331
332    /// Indicates an [`InstrumentNameExchange`] was encountered that was no indexed, so does
333    /// not have a corresponding [`InstrumentIndex`].
334    #[error("InstrumentKey: {0}")]
335    InstrumentKey(String),
336}
337
338#[cfg(test)]
339mod tests {
340    use super::*;
341
342    #[test]
343    fn test_connectivity_error_is_transient() {
344        assert!(ConnectivityError::Timeout.is_transient());
345        assert!(ConnectivityError::Socket("connection refused".into()).is_transient());
346        assert!(ConnectivityError::ExchangeOffline(ExchangeId::BinanceSpot).is_transient());
347    }
348
349    #[test]
350    fn test_client_error_is_transient_connectivity() {
351        let err: ClientError = ClientError::Connectivity(ConnectivityError::Timeout);
352        assert!(err.is_transient());
353
354        let err: ClientError = ClientError::Connectivity(ConnectivityError::Socket("err".into()));
355        assert!(err.is_transient());
356    }
357
358    #[test]
359    fn test_client_error_is_transient_rate_limit() {
360        let err: ClientError = ClientError::Api(ApiError::RateLimit);
361        assert!(err.is_transient());
362    }
363
364    #[test]
365    fn test_client_error_not_transient_api_errors() {
366        let err: ClientError =
367            ClientError::Api(ApiError::AssetInvalid(AssetIndex(0), "bad".into()));
368        assert!(!err.is_transient(), "expected non-transient for {:?}", err);
369
370        let err: ClientError =
371            ClientError::Api(ApiError::BalanceInsufficient(AssetIndex(0), "low".into()));
372        assert!(!err.is_transient(), "expected non-transient for {:?}", err);
373
374        let err: ClientError = ClientError::Api(ApiError::InstrumentInvalid(
375            InstrumentIndex(0),
376            "bad".into(),
377        ));
378        assert!(!err.is_transient(), "expected non-transient for {:?}", err);
379
380        let err: ClientError = ClientError::Api(ApiError::OrderRejected("rejected".into()));
381        assert!(!err.is_transient(), "expected non-transient for {:?}", err);
382
383        let err: ClientError = ClientError::Api(ApiError::OrderAlreadyCancelled);
384        assert!(!err.is_transient(), "expected non-transient for {:?}", err);
385
386        let err: ClientError = ClientError::Api(ApiError::OrderAlreadyFullyFilled);
387        assert!(!err.is_transient(), "expected non-transient for {:?}", err);
388
389        let err: ClientError =
390            ClientError::Api(ApiError::Unauthenticated("invalid signature".into()));
391        assert!(!err.is_transient(), "expected non-transient for {:?}", err);
392    }
393
394    #[test]
395    fn test_client_error_not_transient_task_failed() {
396        let err: ClientError = ClientError::TaskFailed("task panicked".into());
397        assert!(!err.is_transient());
398    }
399
400    #[test]
401    fn test_client_error_not_transient_internal() {
402        let err: ClientError = ClientError::Internal("unknown error".into());
403        assert!(!err.is_transient());
404    }
405
406    #[test]
407    fn test_client_error_not_transient_truncated() {
408        let err: ClientError = ClientError::Truncated { limit: 100 };
409        assert!(!err.is_transient(), "expected non-transient for {:?}", err);
410
411        let err: ClientError = ClientError::TruncatedSnapshot { limit: 500 };
412        assert!(!err.is_transient(), "expected non-transient for {:?}", err);
413    }
414
415    #[test]
416    fn test_client_error_is_transient_exchange_offline() {
417        let err: ClientError =
418            ClientError::Connectivity(ConnectivityError::ExchangeOffline(ExchangeId::BinanceSpot));
419        assert!(err.is_transient(), "expected transient for {:?}", err);
420    }
421
422    #[test]
423    fn test_order_error_is_transient_connectivity() {
424        let err: UnindexedOrderError = OrderError::Connectivity(ConnectivityError::Timeout);
425        assert!(err.is_transient(), "expected transient for {:?}", err);
426
427        let err: UnindexedOrderError =
428            OrderError::Connectivity(ConnectivityError::Socket("connection reset".into()));
429        assert!(err.is_transient(), "expected transient for {:?}", err);
430
431        let err: UnindexedOrderError =
432            OrderError::Connectivity(ConnectivityError::ExchangeOffline(ExchangeId::BinanceSpot));
433        assert!(err.is_transient(), "expected transient for {:?}", err);
434    }
435
436    #[test]
437    fn test_order_error_is_transient_rate_limit() {
438        let err: UnindexedOrderError = OrderError::Rejected(ApiError::RateLimit);
439        assert!(err.is_transient(), "expected transient for {:?}", err);
440    }
441
442    #[test]
443    fn test_order_error_not_transient_api_errors() {
444        let err: UnindexedOrderError =
445            OrderError::Rejected(ApiError::OrderRejected("price out of range".into()));
446        assert!(!err.is_transient(), "expected non-transient for {:?}", err);
447
448        let err: UnindexedOrderError = OrderError::Rejected(ApiError::OrderAlreadyCancelled);
449        assert!(!err.is_transient(), "expected non-transient for {:?}", err);
450
451        let err: UnindexedOrderError = OrderError::Rejected(ApiError::BalanceInsufficient(
452            AssetNameExchange::from("BTC"),
453            "insufficient".into(),
454        ));
455        assert!(!err.is_transient(), "expected non-transient for {:?}", err);
456    }
457}