chronicle_core/error.rs
1//! Domain errors. Every variant carries the concrete values that explain
2//! the failure (ARCHITECTURE.md section 8.1): an opaque variant name is
3//! never the whole diagnostic.
4
5use core::fmt;
6
7use crate::types::OrderId;
8
9/// Errors returned by book construction and by every book command.
10///
11/// Commands that return an error have mutated nothing: no order, level
12/// aggregate, bitmap bit, lookup entry, or sequence number changes when
13/// any of these is returned (INV-ORD-4).
14#[derive(Clone, Debug, PartialEq, Eq)]
15pub enum BookError {
16 /// `BookConfig` failed validation at construction.
17 InvalidConfig {
18 /// The field that failed validation.
19 field: &'static str,
20 /// The rule the field violated.
21 rule: &'static str,
22 },
23 /// An order id of zero was supplied; zero is the lookup sentinel.
24 InvalidOrderId {
25 /// The offending order id (always zero).
26 order_id: OrderId,
27 },
28 /// An active order already uses this id.
29 DuplicateOrderId {
30 /// The conflicting order id.
31 order_id: OrderId,
32 },
33 /// A quantity of zero was supplied on add or amend.
34 InvalidQuantity {
35 /// The offending quantity (always zero).
36 qty: u64,
37 },
38 /// A price failed the tick grid rules of ARCHITECTURE.md section 3.2.
39 InvalidPriceTick {
40 /// The raw price supplied, in quote units.
41 price: u64,
42 /// Configured quote units per tick.
43 tick_size: u64,
44 /// Configured tick value of grid index 0.
45 grid_start_tick: u64,
46 /// Configured number of ticks on the grid.
47 grid_len: u32,
48 },
49 /// Cancel or amend named an order that is not live.
50 OrderNotFound {
51 /// The order id that was not found.
52 order_id: OrderId,
53 },
54 /// The arena has no free slot for an order that would rest.
55 CapacityFull {
56 /// Configured arena capacity.
57 max_orders: u32,
58 },
59 /// The lookup table reached its 50 percent load cap. Defensive: the
60 /// arena cap is enforced first, so this guards the table invariant
61 /// directly rather than relying on that ordering.
62 LookupCapacityFull {
63 /// Table capacity in buckets.
64 capacity: usize,
65 /// Entries present when the insert was refused.
66 len: usize,
67 },
68 /// A Post-Only order price would cross the opposite best price.
69 PostOnlyWouldCross {
70 /// The raw price of the incoming order.
71 price: u64,
72 /// The raw opposite best price it would have crossed.
73 opposite_best: u64,
74 },
75 /// A Fill-or-Kill order could not be filled in full.
76 FokInsufficientLiquidity {
77 /// Quantity the order requested.
78 requested: u64,
79 /// STP-adjusted executable quantity found in preflight.
80 available: u64,
81 },
82 /// An amend would leave the book crossed.
83 AmendWouldCross {
84 /// The order being amended.
85 order_id: OrderId,
86 /// The raw new price requested.
87 new_price: u64,
88 /// The raw opposite best price it would cross.
89 opposite_best: u64,
90 },
91 /// The caller event buffer is smaller than the preflight worst-case
92 /// event count for this command. Returned before any mutation, so
93 /// the identical command can be retried with a larger buffer.
94 EventBufferFull {
95 /// Worst-case events the command could emit.
96 needed: usize,
97 /// Free slots the buffer actually had.
98 available: usize,
99 },
100 /// A checked aggregate operation overflowed. Defensive guard:
101 /// aggregates are sized so this is unreachable in a valid book.
102 ArithmeticOverflow {
103 /// What was being computed when the overflow was detected.
104 what: &'static str,
105 },
106}
107
108impl fmt::Display for BookError {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 match self {
111 Self::InvalidConfig { field, rule } => {
112 write!(
113 f,
114 "invalid book configuration: field {field} violates rule: {rule}"
115 )
116 }
117 Self::InvalidOrderId { order_id } => {
118 write!(f, "invalid order id {}: zero is reserved", order_id.0)
119 }
120 Self::DuplicateOrderId { order_id } => {
121 write!(
122 f,
123 "duplicate order id {}: an active order already uses it",
124 order_id.0
125 )
126 }
127 Self::InvalidQuantity { qty } => {
128 write!(f, "invalid quantity {qty}: quantities must be positive")
129 }
130 Self::InvalidPriceTick {
131 price,
132 tick_size,
133 grid_start_tick,
134 grid_len,
135 } => write!(
136 f,
137 "invalid price tick: price {price} with tick size {tick_size} does not land on the grid starting at tick {grid_start_tick} with {grid_len} ticks"
138 ),
139 Self::OrderNotFound { order_id } => {
140 write!(f, "order not found: no live order with id {}", order_id.0)
141 }
142 Self::CapacityFull { max_orders } => {
143 write!(
144 f,
145 "book capacity full: arena holds at most {max_orders} live orders"
146 )
147 }
148 Self::LookupCapacityFull { capacity, len } => write!(
149 f,
150 "lookup table at load cap: {len} entries in {capacity} buckets (cap is 50 percent)"
151 ),
152 Self::PostOnlyWouldCross {
153 price,
154 opposite_best,
155 } => write!(
156 f,
157 "post-only order at price {price} would cross the opposite best price {opposite_best}"
158 ),
159 Self::FokInsufficientLiquidity {
160 requested,
161 available,
162 } => write!(
163 f,
164 "fill-or-kill order requested {requested} but only {available} is executable"
165 ),
166 Self::AmendWouldCross {
167 order_id,
168 new_price,
169 opposite_best,
170 } => write!(
171 f,
172 "amend of order {} to price {new_price} would cross the opposite best price {opposite_best}",
173 order_id.0
174 ),
175 Self::EventBufferFull { needed, available } => write!(
176 f,
177 "event buffer full: command needs up to {needed} event slots, buffer has {available}"
178 ),
179 Self::ArithmeticOverflow { what } => {
180 write!(f, "arithmetic overflow while computing {what}")
181 }
182 }
183 }
184}
185
186impl core::error::Error for BookError {}