Skip to main content

nautilus_common/cache/
error.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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
16use nautilus_model::identifiers::{
17    AccountId, ClientOrderId, InstrumentId, OrderListId, PositionId, VenueOrderId,
18};
19use thiserror::Error;
20use ustr::Ustr;
21
22/// Message used for a missing account lookup.
23pub const ACCOUNT_NOT_FOUND: &str = "account not found in cache";
24
25/// Message used for a missing currency lookup.
26pub const CURRENCY_NOT_FOUND: &str = "currency not found in cache";
27
28/// Message used for a missing instrument lookup.
29pub const INSTRUMENT_NOT_FOUND: &str = "instrument not found in cache";
30
31/// Message used for a missing order book lookup.
32pub const ORDER_BOOK_NOT_FOUND: &str = "order book not found in cache";
33
34/// Message used for a missing own order book lookup.
35pub const OWN_ORDER_BOOK_NOT_FOUND: &str = "own order book not found in cache";
36
37/// Message used for a missing synthetic instrument lookup.
38pub const SYNTHETIC_INSTRUMENT_NOT_FOUND: &str = "synthetic instrument not found in cache";
39
40/// Message used for a missing order lookup.
41pub const ORDER_NOT_FOUND: &str = "order not found in cache";
42
43/// Message used for a missing order list lookup.
44pub const ORDER_LIST_NOT_FOUND: &str = "order list not found in cache";
45
46/// Message used for a missing position lookup.
47pub const POSITION_NOT_FOUND: &str = "position not found in cache";
48
49/// Error returned when a venue order ID is already owned by another client order.
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
51#[error(
52    "Venue order ID {venue_order_id} is already owned by client order \
53     {existing_client_order_id} and cannot be claimed by {claimant_client_order_id}"
54)]
55pub struct VenueOrderIdOwnershipError {
56    /// The venue order ID whose ownership conflicts.
57    pub venue_order_id: VenueOrderId,
58    /// The client order which already owns the venue order ID.
59    pub existing_client_order_id: ClientOrderId,
60    /// The client order attempting to claim the venue order ID.
61    pub claimant_client_order_id: ClientOrderId,
62}
63
64/// Error returned when an account cannot be resolved from a cache or store.
65#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
66pub enum AccountLookupError {
67    /// The requested account is not present.
68    #[error("{message}: {account_id}", message = ACCOUNT_NOT_FOUND)]
69    NotFound {
70        /// The account identifier that was requested.
71        account_id: AccountId,
72    },
73}
74
75impl AccountLookupError {
76    /// Returns a not-found error for `account_id`.
77    #[must_use]
78    pub const fn not_found(account_id: AccountId) -> Self {
79        Self::NotFound { account_id }
80    }
81}
82
83/// Error returned when a currency cannot be resolved from a cache or store.
84#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
85pub enum CurrencyLookupError {
86    /// The requested currency is not present.
87    #[error("{message}: {code}", message = CURRENCY_NOT_FOUND)]
88    NotFound {
89        /// The currency code that was requested.
90        code: Ustr,
91    },
92}
93
94impl CurrencyLookupError {
95    /// Returns a not-found error for `code`.
96    #[must_use]
97    pub const fn not_found(code: Ustr) -> Self {
98        Self::NotFound { code }
99    }
100}
101
102/// Error returned when an instrument cannot be resolved from a cache or store.
103#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
104pub enum InstrumentLookupError {
105    /// The requested instrument is not present.
106    #[error("{message}: {instrument_id}", message = INSTRUMENT_NOT_FOUND)]
107    NotFound {
108        /// The instrument identifier that was requested.
109        instrument_id: InstrumentId,
110    },
111}
112
113impl InstrumentLookupError {
114    /// Returns a not-found error for `instrument_id`.
115    #[must_use]
116    pub const fn not_found(instrument_id: InstrumentId) -> Self {
117        Self::NotFound { instrument_id }
118    }
119}
120
121/// Error returned when a synthetic instrument cannot be resolved from a cache or store.
122#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
123pub enum SyntheticInstrumentLookupError {
124    /// The requested synthetic instrument is not present.
125    #[error("{message}: {instrument_id}", message = SYNTHETIC_INSTRUMENT_NOT_FOUND)]
126    NotFound {
127        /// The instrument identifier that was requested.
128        instrument_id: InstrumentId,
129    },
130}
131
132impl SyntheticInstrumentLookupError {
133    /// Returns a not-found error for `instrument_id`.
134    #[must_use]
135    pub const fn not_found(instrument_id: InstrumentId) -> Self {
136        Self::NotFound { instrument_id }
137    }
138}
139
140/// Error returned when an order book cannot be resolved from a cache or store.
141#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
142pub enum OrderBookLookupError {
143    /// The requested order book is not present.
144    #[error("{message}: {instrument_id}", message = ORDER_BOOK_NOT_FOUND)]
145    NotFound {
146        /// The instrument identifier that was requested.
147        instrument_id: InstrumentId,
148    },
149}
150
151impl OrderBookLookupError {
152    /// Returns a not-found error for `instrument_id`.
153    #[must_use]
154    pub const fn not_found(instrument_id: InstrumentId) -> Self {
155        Self::NotFound { instrument_id }
156    }
157}
158
159/// Error returned when an own order book cannot be resolved from a cache or store.
160#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
161pub enum OwnOrderBookLookupError {
162    /// The requested own order book is not present.
163    #[error("{message}: {instrument_id}", message = OWN_ORDER_BOOK_NOT_FOUND)]
164    NotFound {
165        /// The instrument identifier that was requested.
166        instrument_id: InstrumentId,
167    },
168}
169
170impl OwnOrderBookLookupError {
171    /// Returns a not-found error for `instrument_id`.
172    #[must_use]
173    pub const fn not_found(instrument_id: InstrumentId) -> Self {
174        Self::NotFound { instrument_id }
175    }
176}
177
178/// Error returned when an order cannot be resolved from a cache or store.
179#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
180pub enum OrderLookupError {
181    /// The requested order is not present.
182    #[error("{message}: {client_order_id}", message = ORDER_NOT_FOUND)]
183    NotFound {
184        /// The client order identifier that was requested.
185        client_order_id: ClientOrderId,
186    },
187}
188
189impl OrderLookupError {
190    /// Returns a not-found error for `client_order_id`.
191    #[must_use]
192    pub const fn not_found(client_order_id: ClientOrderId) -> Self {
193        Self::NotFound { client_order_id }
194    }
195}
196
197/// Error returned when an order list cannot be resolved from a cache or store.
198#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
199pub enum OrderListLookupError {
200    /// The requested order list is not present.
201    #[error("{message}: {order_list_id}", message = ORDER_LIST_NOT_FOUND)]
202    NotFound {
203        /// The order list identifier that was requested.
204        order_list_id: OrderListId,
205    },
206}
207
208impl OrderListLookupError {
209    /// Returns a not-found error for `order_list_id`.
210    #[must_use]
211    pub const fn not_found(order_list_id: OrderListId) -> Self {
212        Self::NotFound { order_list_id }
213    }
214}
215
216/// Error returned when a position cannot be resolved from a cache or store.
217#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
218pub enum PositionLookupError {
219    /// The requested position is not present.
220    #[error("{message}: {position_id}", message = POSITION_NOT_FOUND)]
221    NotFound {
222        /// The position identifier that was requested.
223        position_id: PositionId,
224    },
225}
226
227impl PositionLookupError {
228    /// Returns a not-found error for `position_id`.
229    #[must_use]
230    pub const fn not_found(position_id: PositionId) -> Self {
231        Self::NotFound { position_id }
232    }
233}