rithmic_rs/error.rs
1use std::fmt;
2
3/// Typed errors returned by all plant handle methods.
4///
5/// ```ignore
6/// match handle.subscribe("ESH6", "CME").await {
7/// Ok(resp) => { /* success */ }
8/// Err(RithmicError::ConnectionClosed | RithmicError::SendFailed) => {
9/// handle.abort();
10/// // reconnect — see examples/reconnect.rs
11/// }
12/// Err(RithmicError::ServerError(msg)) => eprintln!("rejected: {msg}"),
13/// Err(e) => eprintln!("{e}"),
14/// }
15/// ```
16#[derive(Debug, Clone)]
17#[non_exhaustive]
18pub enum RithmicError {
19 /// WebSocket connection could not be established.
20 ConnectionFailed(String),
21 /// The plant's WebSocket connection is gone; pending requests will never complete.
22 ConnectionClosed,
23 /// WebSocket send failed after the request was registered.
24 SendFailed,
25 /// Server returned an empty response where at least one was expected.
26 EmptyResponse,
27 /// Protocol-level rejection from Rithmic (the `rp_code` text).
28 ServerError(String),
29}
30
31impl fmt::Display for RithmicError {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33 match self {
34 RithmicError::ConnectionFailed(msg) => write!(f, "connection failed: {msg}"),
35 RithmicError::ConnectionClosed => write!(f, "connection closed"),
36 RithmicError::SendFailed => write!(f, "WebSocket send failed"),
37 RithmicError::EmptyResponse => write!(f, "empty response"),
38 RithmicError::ServerError(msg) => write!(f, "server error: {msg}"),
39 }
40 }
41}
42
43impl std::error::Error for RithmicError {}