Skip to main content

perpl_sdk/state/l3_book/
error.rs

1//! Error types for order book operations.
2
3use fastnum::UD64;
4use thiserror::Error;
5
6use crate::types::{OrderId, OrderSide};
7
8/// Error type for order book operations.
9#[derive(Debug, Clone, PartialEq, Error)]
10pub enum OrderBookError {
11    /// Attempted to add an order that already exists in the book.
12    #[error("order #{order_id} already exists at price {existing_price}")]
13    OrderAlreadyExists { order_id: OrderId, existing_price: UD64 },
14
15    /// Attempted to update or remove an order that doesn't exist.
16    #[error("order #{order_id} not found in book")]
17    OrderNotFound { order_id: OrderId },
18
19    /// Order exists in index but not found at the expected price level.
20    /// This indicates internal inconsistency.
21    #[error("order #{order_id} not found at expected {side:?} level price {expected_price}")]
22    OrderNotAtExpectedLevel { order_id: OrderId, expected_price: UD64, side: OrderSide },
23
24    /// Attempted to update an order but the new order has a different ID.
25    #[error("order ID mismatch: expected {expected}, got {actual}")]
26    OrderIdMismatch { expected: OrderId, actual: OrderId },
27
28    /// Order has zero or negative size.
29    #[error("order #{order_id} has invalid size: {size}")]
30    InvalidOrderSize { order_id: OrderId, size: UD64 },
31
32    /// Order has zero price.
33    #[error("order #{order_id} has invalid price: {price}")]
34    InvalidOrderPrice { order_id: OrderId, price: UD64 },
35
36    /// Expected price level not found. This indicates internal inconsistency.
37    #[error("level not found at price {price} ({side:?} side)")]
38    LevelNotFound { price: UD64, side: OrderSide },
39
40    /// Order references another order that doesn't exist in the snapshot.
41    /// This indicates data inconsistency.
42    #[error(
43        "order #{order_id} has dangling {pointer} reference to non-existent order #{referenced_id}"
44    )]
45    DanglingOrderReference { order_id: OrderId, referenced_id: OrderId, pointer: &'static str },
46}
47
48/// Result type for OrderBook operations.
49pub type OrderBookResult<T> = Result<T, OrderBookError>;