@@ -21,7 +21,7 @@ use ahash::{AHashMap, AHashSet};
2121use indexmap:: { IndexMap , IndexSet } ;
2222use nautilus_analysis:: analyzer:: PortfolioAnalyzer ;
2323use nautilus_common:: {
24- cache:: Cache ,
24+ cache:: { AccountLookupError , Cache } ,
2525 clock:: Clock ,
2626 enums:: LogColor ,
2727 msgbus:: { self , MessagingSwitchboard , TypedHandler , TypedIntoHandler } ,
@@ -1111,14 +1111,12 @@ impl Portfolio {
11111111
11121112 for position in & positions_open {
11131113 // Get account for THIS position
1114- let account = if let Some ( account) = cache. account ( & position. account_id ) {
1115- account
1116- } else {
1117- log:: error!(
1118- "Cannot calculate net exposure: no account for {}" ,
1119- position. account_id
1120- ) ;
1121- return None ;
1114+ let account = match cache. try_account ( & position. account_id ) {
1115+ Ok ( account) => account,
1116+ Err ( e) => {
1117+ log:: error!( "Cannot calculate net exposure: {e}" ) ;
1118+ return None ;
1119+ }
11221120 } ;
11231121
11241122 // Validate consistent base currency across accounts
@@ -2247,11 +2245,12 @@ fn update_order(
22472245 let ( instrument, orders_open) = {
22482246 let cache_ref = cache. borrow ( ) ;
22492247
2250- let account = if let Some ( account) = cache_ref. account ( & account_id) {
2251- account
2252- } else {
2253- log:: error!( "Cannot update order: no account registered for {account_id}" ) ;
2254- return ;
2248+ let account = match cache_ref. try_account ( & account_id) {
2249+ Ok ( account) => account,
2250+ Err ( e) => {
2251+ log:: error!( "Cannot update order: {e}" ) ;
2252+ return ;
2253+ }
22552254 } ;
22562255
22572256 match & * account {
@@ -2325,11 +2324,15 @@ fn update_order(
23252324 } ;
23262325
23272326 // No cache borrow held: AccountsManager borrows cache internally for xrate lookups.
2328- let mut working_account = if let Some ( account) = cache. borrow_mut ( ) . take_account ( & account_id) {
2329- account
2330- } else {
2331- log:: error!( "Cannot update order: no account registered for {account_id}" ) ;
2332- return ;
2327+ let mut working_account = match cache. borrow_mut ( ) . take_account ( & account_id) {
2328+ Some ( account) => account,
2329+ None => {
2330+ log:: error!(
2331+ "Cannot update order: {}" ,
2332+ AccountLookupError :: not_found( account_id)
2333+ ) ;
2334+ return ;
2335+ }
23332336 } ;
23342337
23352338 if let OrderEventAny :: Filled ( order_filled) = event {
0 commit comments