1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! The unified error taxonomy.
//!
//! Every exchange maps its own numeric/string error codes onto this single enum,
//! so a consumer can react to a class of failure (`RateLimited`, `InsufficientBalance`,
//! `InvalidSymbol`, …) without knowing which venue produced it. Variants that
//! originate at a venue keep the raw `code`/`message` for diagnostics.
use crate::options::MarketType;
use std::time::Duration;
/// The crate-wide result type.
pub type Result<T> = std::result::Result<T, Error>;
/// A unified connectivity / execution error.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// No exchange is registered under the given name.
#[error("unsupported exchange: {0}")]
UnsupportedExchange(String),
/// A symbol string could not be parsed or is not listed on the venue.
#[error("invalid symbol: {0}")]
InvalidSymbol(String),
/// Credentials are missing a field the venue requires (e.g. a passphrase).
#[error("invalid credentials: {0}")]
InvalidCredentials(&'static str),
/// An order request is malformed before it is ever sent.
#[error("invalid order: {0}")]
InvalidOrder(&'static str),
/// An order would violate one of the venue's symbol filters
/// (lot size, price tick, min-notional, …).
#[error("order violates exchange filter: {0}")]
Filter(String),
/// Request signing or authentication was rejected by the venue.
#[error("authentication failed: {0}")]
Auth(String),
/// The account has insufficient balance for the request.
#[error("insufficient balance")]
InsufficientBalance,
/// The venue rate-limited the request; `retry_after` is the advised wait if
/// the venue supplied one (`Retry-After`).
#[error("rate limited{}", .retry_after.map(|d| format!(" (retry after {:.1}s)", d.as_secs_f64())).unwrap_or_default())]
RateLimited {
/// The advised back-off, if the venue provided one.
retry_after: Option<Duration>,
},
/// The venue rejected the order after it was accepted for processing.
#[error("order rejected by exchange (code {code}): {message}")]
OrderRejected {
/// The venue's error code, verbatim.
code: String,
/// The venue's human-readable message.
message: String,
},
/// Any other error reported by the venue, kept verbatim for diagnostics.
#[error("exchange error (code {code}): {message}")]
Exchange {
/// The venue's error code, verbatim.
code: String,
/// The venue's human-readable message.
message: String,
},
/// A requested entity (order, symbol metadata, …) was not found.
#[error("not found: {0}")]
NotFound(String),
/// The request timed out before a response was received.
#[error("request timed out")]
Timeout,
/// A stream or client operation was attempted before connecting.
#[error("not connected")]
NotConnected,
/// A transport-level (socket/TLS/DNS) failure.
#[error("network error: {0}")]
Network(String),
/// A response could not be parsed into the expected shape.
#[error("deserialization error: {0}")]
Deserialization(String),
}
impl Error {
/// A trigger order on a venue path that cannot carry the trigger price.
///
/// Placing it anyway is not a smaller version of the order the caller
/// asked for -- a stop-loss with no trigger executes at once, at the price
/// it was meant to protect against. So the paths that cannot express the
/// trigger refuse, and this is the refusal.
pub(crate) fn unsupported_trigger(venue: &str) -> Self {
Error::Exchange {
code: "unsupported".to_string(),
message: format!(
"{venue}: trigger (stop) orders are not implemented on this path; sending one without its trigger price would execute immediately instead of resting"
),
}
}
/// A market this client does not route.
///
/// [`MarketType`](crate::MarketType) names four markets, and a venue client
/// that quietly serves a different one than the caller asked for is the
/// same defect as an order that quietly drops a field: the answer looks
/// right and describes something else. Binance's spot host answers
/// `BTCUSD` -- it is a real spot pair -- so a coin-margined request there
/// returned real, wrong prices, and would have placed a real, wrong order.
///
/// So a market a client does not route is refused before anything is sent.
pub(crate) fn unsupported_market(venue: &str, market: MarketType) -> Self {
Error::Exchange {
code: "unsupported".to_string(),
message: format!(
"{venue}: {} is not routed by this client; the request would reach a different market than the one asked for",
market.describe()
),
}
}
/// An order field the caller set that this venue path cannot express.
///
/// The same reasoning as [`Error::unsupported_trigger`], generalised to the
/// rest of the request. A field the caller set is part of what the order
/// *is*: an `Ioc` that is sent as a `Gtc` rests in the book the caller
/// asked it never to rest in, and a `post_only` that is dropped can take
/// liquidity at the taker fee. Sending the order without the field is not
/// a smaller version of it, so the paths that cannot express a field refuse
/// instead of quietly weakening the order.
pub(crate) fn unsupported_field(venue: &str, field: &str, detail: &str) -> Self {
Error::Exchange {
code: "unsupported".to_string(),
message: format!(
"{venue}: {field} cannot be expressed on this order path ({detail}); sending the order without it would place a different order than requested"
),
}
}
/// Whether retrying the operation could plausibly succeed: transient
/// transport failures, timeouts and rate limits. Permanent failures
/// (invalid order, auth, insufficient balance, …) return `false`.
#[must_use]
pub fn is_retryable(&self) -> bool {
matches!(
self,
Error::RateLimited { .. } | Error::Timeout | Error::Network(_)
)
}
/// The venue-supplied error code, if this error originated at an exchange.
#[must_use]
pub fn code(&self) -> Option<&str> {
match self {
Error::OrderRejected { code, .. } | Error::Exchange { code, .. } => Some(code),
_ => None,
}
}
/// Attach the venue's advised wait to a rate-limit error that was raised
/// without one.
///
/// The venue clients recognise a rate limit from the *body* -- a numeric
/// code in the error envelope -- while the wait arrives in the
/// `Retry-After` *header*. The two are read in different places, so this
/// joins them at the call site that has both.
///
/// Every other variant is returned untouched, and a `RateLimited` that
/// already carries a wait keeps it: a venue that states the delay in its
/// body is more specific than the header, and this must not overwrite it.
#[must_use]
pub fn with_retry_after(self, retry_after: Option<Duration>) -> Self {
match self {
Error::RateLimited { retry_after: None } => Error::RateLimited { retry_after },
other => other,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn retryable_classification() {
assert!(Error::Timeout.is_retryable());
assert!(Error::Network("reset".into()).is_retryable());
assert!(Error::RateLimited { retry_after: None }.is_retryable());
assert!(!Error::InsufficientBalance.is_retryable());
assert!(!Error::InvalidOrder("no price").is_retryable());
assert!(!Error::Auth("bad signature".into()).is_retryable());
}
#[test]
fn code_is_exposed_only_for_venue_errors() {
let rejected = Error::OrderRejected {
code: "-2010".into(),
message: "insufficient balance".into(),
};
assert_eq!(rejected.code(), Some("-2010"));
let other = Error::Exchange {
code: "1021".into(),
message: "timestamp outside recvWindow".into(),
};
assert_eq!(other.code(), Some("1021"));
assert_eq!(Error::Timeout.code(), None);
assert_eq!(Error::InvalidSymbol("??".into()).code(), None);
}
#[test]
fn display_renders_each_variant() {
assert_eq!(
Error::UnsupportedExchange("foo".into()).to_string(),
"unsupported exchange: foo"
);
assert_eq!(
Error::InvalidSymbol("BTC".into()).to_string(),
"invalid symbol: BTC"
);
assert_eq!(
Error::InvalidCredentials("passphrase required").to_string(),
"invalid credentials: passphrase required"
);
assert_eq!(
Error::InvalidOrder("limit order needs a price").to_string(),
"invalid order: limit order needs a price"
);
assert_eq!(
Error::Filter("LOT_SIZE: step 0.001".into()).to_string(),
"order violates exchange filter: LOT_SIZE: step 0.001"
);
assert_eq!(
Error::Auth("bad signature".into()).to_string(),
"authentication failed: bad signature"
);
assert_eq!(
Error::InsufficientBalance.to_string(),
"insufficient balance"
);
assert_eq!(
Error::RateLimited { retry_after: None }.to_string(),
"rate limited"
);
assert_eq!(
Error::RateLimited {
retry_after: Some(Duration::from_millis(1500))
}
.to_string(),
"rate limited (retry after 1.5s)"
);
assert_eq!(
Error::OrderRejected {
code: "-2010".into(),
message: "rejected".into()
}
.to_string(),
"order rejected by exchange (code -2010): rejected"
);
assert_eq!(
Error::Exchange {
code: "1".into(),
message: "oops".into()
}
.to_string(),
"exchange error (code 1): oops"
);
assert_eq!(
Error::NotFound("order 7".into()).to_string(),
"not found: order 7"
);
assert_eq!(Error::Timeout.to_string(), "request timed out");
assert_eq!(Error::NotConnected.to_string(), "not connected");
assert_eq!(
Error::Network("eof".into()).to_string(),
"network error: eof"
);
assert_eq!(
Error::Deserialization("bad json".into()).to_string(),
"deserialization error: bad json"
);
}
#[test]
fn with_retry_after_fills_an_empty_rate_limit() {
let filled = Error::RateLimited { retry_after: None }
.with_retry_after(Some(Duration::from_millis(2500)));
let expected = Some(Duration::from_millis(2500));
assert!(matches!(filled, Error::RateLimited { retry_after } if retry_after == expected));
}
#[test]
fn with_retry_after_does_not_overwrite_what_the_venue_already_said() {
// A venue that states the delay in its body is more specific than the
// header, so the header must not replace it.
let body_said = Error::RateLimited {
retry_after: Some(Duration::from_secs(60)),
};
let kept = body_said.with_retry_after(Some(Duration::from_secs(1)));
let expected = Some(Duration::from_secs(60));
assert!(matches!(kept, Error::RateLimited { retry_after } if retry_after == expected));
}
#[test]
fn with_retry_after_leaves_every_other_variant_alone() {
let timeout = Error::Timeout.with_retry_after(Some(Duration::from_secs(5)));
assert!(matches!(timeout, Error::Timeout));
let balance = Error::InsufficientBalance.with_retry_after(Some(Duration::from_secs(5)));
assert!(matches!(balance, Error::InsufficientBalance));
}
}