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,
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 an account cannot be resolved from a cache or store.
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
51pub enum AccountLookupError {
52    /// The requested account is not present.
53    #[error("{message}: {account_id}", message = ACCOUNT_NOT_FOUND)]
54    NotFound {
55        /// The account identifier that was requested.
56        account_id: AccountId,
57    },
58}
59
60impl AccountLookupError {
61    /// Returns a not-found error for `account_id`.
62    #[must_use]
63    pub const fn not_found(account_id: AccountId) -> Self {
64        Self::NotFound { account_id }
65    }
66}
67
68/// Error returned when a currency cannot be resolved from a cache or store.
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
70pub enum CurrencyLookupError {
71    /// The requested currency is not present.
72    #[error("{message}: {code}", message = CURRENCY_NOT_FOUND)]
73    NotFound {
74        /// The currency code that was requested.
75        code: Ustr,
76    },
77}
78
79impl CurrencyLookupError {
80    /// Returns a not-found error for `code`.
81    #[must_use]
82    pub const fn not_found(code: Ustr) -> Self {
83        Self::NotFound { code }
84    }
85}
86
87/// Error returned when an instrument cannot be resolved from a cache or store.
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
89pub enum InstrumentLookupError {
90    /// The requested instrument is not present.
91    #[error("{message}: {instrument_id}", message = INSTRUMENT_NOT_FOUND)]
92    NotFound {
93        /// The instrument identifier that was requested.
94        instrument_id: InstrumentId,
95    },
96}
97
98impl InstrumentLookupError {
99    /// Returns a not-found error for `instrument_id`.
100    #[must_use]
101    pub const fn not_found(instrument_id: InstrumentId) -> Self {
102        Self::NotFound { instrument_id }
103    }
104}
105
106/// Error returned when a synthetic instrument cannot be resolved from a cache or store.
107#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
108pub enum SyntheticInstrumentLookupError {
109    /// The requested synthetic instrument is not present.
110    #[error("{message}: {instrument_id}", message = SYNTHETIC_INSTRUMENT_NOT_FOUND)]
111    NotFound {
112        /// The instrument identifier that was requested.
113        instrument_id: InstrumentId,
114    },
115}
116
117impl SyntheticInstrumentLookupError {
118    /// Returns a not-found error for `instrument_id`.
119    #[must_use]
120    pub const fn not_found(instrument_id: InstrumentId) -> Self {
121        Self::NotFound { instrument_id }
122    }
123}
124
125/// Error returned when an order book cannot be resolved from a cache or store.
126#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
127pub enum OrderBookLookupError {
128    /// The requested order book is not present.
129    #[error("{message}: {instrument_id}", message = ORDER_BOOK_NOT_FOUND)]
130    NotFound {
131        /// The instrument identifier that was requested.
132        instrument_id: InstrumentId,
133    },
134}
135
136impl OrderBookLookupError {
137    /// Returns a not-found error for `instrument_id`.
138    #[must_use]
139    pub const fn not_found(instrument_id: InstrumentId) -> Self {
140        Self::NotFound { instrument_id }
141    }
142}
143
144/// Error returned when an own order book cannot be resolved from a cache or store.
145#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
146pub enum OwnOrderBookLookupError {
147    /// The requested own order book is not present.
148    #[error("{message}: {instrument_id}", message = OWN_ORDER_BOOK_NOT_FOUND)]
149    NotFound {
150        /// The instrument identifier that was requested.
151        instrument_id: InstrumentId,
152    },
153}
154
155impl OwnOrderBookLookupError {
156    /// Returns a not-found error for `instrument_id`.
157    #[must_use]
158    pub const fn not_found(instrument_id: InstrumentId) -> Self {
159        Self::NotFound { instrument_id }
160    }
161}
162
163/// Error returned when an order cannot be resolved from a cache or store.
164#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
165pub enum OrderLookupError {
166    /// The requested order is not present.
167    #[error("{message}: {client_order_id}", message = ORDER_NOT_FOUND)]
168    NotFound {
169        /// The client order identifier that was requested.
170        client_order_id: ClientOrderId,
171    },
172}
173
174impl OrderLookupError {
175    /// Returns a not-found error for `client_order_id`.
176    #[must_use]
177    pub const fn not_found(client_order_id: ClientOrderId) -> Self {
178        Self::NotFound { client_order_id }
179    }
180}
181
182/// Error returned when an order list cannot be resolved from a cache or store.
183#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
184pub enum OrderListLookupError {
185    /// The requested order list is not present.
186    #[error("{message}: {order_list_id}", message = ORDER_LIST_NOT_FOUND)]
187    NotFound {
188        /// The order list identifier that was requested.
189        order_list_id: OrderListId,
190    },
191}
192
193impl OrderListLookupError {
194    /// Returns a not-found error for `order_list_id`.
195    #[must_use]
196    pub const fn not_found(order_list_id: OrderListId) -> Self {
197        Self::NotFound { order_list_id }
198    }
199}
200
201/// Error returned when a position cannot be resolved from a cache or store.
202#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
203pub enum PositionLookupError {
204    /// The requested position is not present.
205    #[error("{message}: {position_id}", message = POSITION_NOT_FOUND)]
206    NotFound {
207        /// The position identifier that was requested.
208        position_id: PositionId,
209    },
210}
211
212impl PositionLookupError {
213    /// Returns a not-found error for `position_id`.
214    #[must_use]
215    pub const fn not_found(position_id: PositionId) -> Self {
216        Self::NotFound { position_id }
217    }
218}