Skip to content

Commit 5cf5441

Browse files
committed
cleans up initial condition and hotpath errors with thiserror
1 parent 3e270f1 commit 5cf5441

File tree

2 files changed

+36
-49
lines changed

2 files changed

+36
-49
lines changed

quoter-errors/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ edition = "2021"
77

88
[dependencies]
99
reqwest = "0.11.17"
10+
serde_json = "1.0.142"
1011
thiserror = "1.0.40"
1112
tokio-tungstenite = "0.18.0"

quoter-errors/src/lib.rs

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,61 @@
1-
use std::fmt;
21
use thiserror::Error;
32

43
#[derive(Error, Debug)]
54
pub enum ErrorHotPath {
5+
#[error("Exchange WS Error: {0}")]
66
ExchangeWSError(String),
7+
#[error("Exchange WS Reconnect Error: {0}")]
78
ExchangeWSReconnectError(String),
9+
#[error("Serialization error: {0}")]
810
Serialization(String),
11+
#[error("Quoter GRPC Error: {0}")]
912
QuoterGRPCError(String),
13+
#[error("Quote Server Sink Error: {0}")]
1014
QuoteServerSinkError(String),
15+
#[error("HTTP Snapshot error: {0}")]
1116
ExchangeStreamSnapshot(String),
17+
#[error("Received non-text message from exchange")]
1218
ReceivedNonTextMessageFromExchange,
19+
#[error("OrderBook Error: {0}")]
1320
OrderBook(String),
21+
#[error("Negative liquidity detected")]
1422
OrderBookNegativeLiquidity,
23+
#[error("Maximum traversal limit reached")]
1524
OrderBookMaxTraversedReached,
25+
#[error("Deal send failed")]
1626
OrderBookDealSendFail,
1727
}
1828

19-
impl fmt::Display for ErrorHotPath {
20-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21-
match self {
22-
ErrorHotPath::ExchangeWSError(s) => write!(f, "Exchange WS Error: {}", s),
23-
ErrorHotPath::ExchangeWSReconnectError(s) => {
24-
write!(f, "Exchange WS Reconnect Error: {}", s)
25-
}
26-
ErrorHotPath::QuoterGRPCError(s) => write!(f, "Quoter GRPC Error: {}", s),
27-
ErrorHotPath::QuoteServerSinkError(s) => write!(f, "Quote Server Sink Error: {}", s),
28-
ErrorHotPath::Serialization(s) => write!(f, "Serialization error: {}", s),
29-
ErrorHotPath::ReceivedNonTextMessageFromExchange => {
30-
write!(f, "ReceivedNonTextMessage")
31-
}
32-
ErrorHotPath::OrderBook(s) => {
33-
write!(f, "OrderBookError: {}", s)
34-
}
35-
ErrorHotPath::ExchangeStreamSnapshot(s) => {
36-
write!(f, "HTTPSnapshot error: {}", s)
37-
}
38-
ErrorHotPath::OrderBookNegativeLiquidity => {
39-
write!(f, "NegativeQuantity")
40-
}
41-
ErrorHotPath::OrderBookMaxTraversedReached => {
42-
write!(f, "MaxTraversedReached")
43-
}
44-
ErrorHotPath::OrderBookDealSendFail => {
45-
write!(f, "DealSendFail")
46-
}
47-
}
48-
}
49-
}
50-
51-
// TODO: Update these errors to take in the upstream error
5229
#[derive(Error, Debug)]
5330
pub enum ErrorInitialState {
54-
ExchangeWSError(tokio_tungstenite::tungstenite::Error),
31+
#[error("Exchange WS Error: {0}")]
32+
ExchangeWSError(#[from] tokio_tungstenite::tungstenite::Error),
33+
#[error("Message Serialization Error: {0}")]
5534
MessageSerialization(String),
35+
#[error("WS Connection Error: {0}")]
5636
WSConnection(String),
37+
#[error("Exchange Snapshot error: {0}")]
5738
Snapshot(String),
39+
#[error("Exchange Controller Error")]
5840
ExchangeController,
5941
}
6042

61-
impl fmt::Display for ErrorInitialState {
62-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63-
match self {
64-
ErrorInitialState::ExchangeWSError(e) => {
65-
write!(f, "Exchange WS Error: {}", e)
66-
}
67-
ErrorInitialState::MessageSerialization(s) => {
68-
write!(f, "Message Serialization Error: {}", s)
69-
}
70-
ErrorInitialState::WSConnection(s) => write!(f, "WS Connection Error: {}", s),
71-
ErrorInitialState::ExchangeController => write!(f, "Exchange Controller Error"),
72-
ErrorInitialState::Snapshot(s) => write!(f, "Exchange Snapshot error: {}", s),
73-
}
74-
}
43+
#[derive(Error, Debug)]
44+
pub enum ErrorWithSource {
45+
#[error("Network error")]
46+
Network(#[from] tokio_tungstenite::tungstenite::Error),
47+
#[error("Serialization failed")]
48+
Serialization(#[from] serde_json::Error),
49+
#[error("IO error: {message}")]
50+
Io {
51+
#[source]
52+
source: std::io::Error,
53+
message: String,
54+
},
55+
#[error("Custom error with context: {context}")]
56+
Custom {
57+
context: String,
58+
#[source]
59+
source: Box<dyn std::error::Error + Send + Sync>,
60+
},
7561
}

0 commit comments

Comments
 (0)