nautilus_model/orderbook/
error.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Errors associated with order book operations and integrity.
17
18use nautilus_core::UnixNanos;
19
20use super::ladder::BookPrice;
21use crate::{
22    enums::{BookType, OrderSide},
23    identifiers::InstrumentId,
24};
25
26#[derive(thiserror::Error, Debug, PartialEq)]
27pub enum InvalidBookOperation {
28    #[error("Invalid book operation: cannot pre-process order for {0} book")]
29    PreProcessOrder(BookType),
30    #[error("Invalid book operation: cannot add order for {0} book")]
31    Add(BookType),
32    #[error("Invalid book operation: cannot update with tick for {0} book")]
33    Update(BookType),
34}
35
36#[derive(thiserror::Error, Debug, PartialEq)]
37pub enum BookIntegrityError {
38    #[error("Integrity error: order not found: order_id={0}, sequence={1}, ts_event={2}")]
39    OrderNotFound(u64, u64, UnixNanos),
40    #[error("Integrity error: invalid `NoOrderSide` in book")]
41    NoOrderSide,
42    #[error("Integrity error: order_id={0} not found in book for side resolution")]
43    OrderNotFoundForSideResolution(u64),
44    #[error("Integrity error: orders in cross [{0} {1}]")]
45    OrdersCrossed(BookPrice, BookPrice),
46    #[error("Integrity error: number of {0} orders at level > 1 for L2_MBP book, was {1}")]
47    TooManyOrders(OrderSide, usize),
48    #[error("Integrity error: number of {0} levels > 1 for L1_MBP book, was {1}")]
49    TooManyLevels(OrderSide, usize),
50    #[error("Integrity error: instrument ID mismatch: book={0}, delta={1}")]
51    InstrumentMismatch(InstrumentId, InstrumentId),
52}