{"record":{"id":"84316185503da922","repo":"nautechsystems/nautilus_trader","slug":"failed-to-load-order-events-e","errorCode":null,"errorMessage":"Failed to load order events: {e}","messagePattern":"Failed to load order events: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":805,"sourceCode":"    ///\n    /// Returns an error if assembling events or SQL operations fail.\n    pub async fn load_order(\n        pool: &PgPool,\n        client_order_id: &ClientOrderId,\n    ) -> anyhow::Result<Option<OrderAny>> {\n        let order_events = Self::load_order_events(pool, client_order_id).await;\n\n        match order_events {\n            Ok(order_events) => {\n                if order_events.is_empty() {\n                    return Ok(None);\n                }\n                let order = OrderAny::from_events(order_events).map_err(|e| {\n                    anyhow::anyhow!(\"Failed to assemble order {client_order_id} from events: {e}\")\n                })?;\n                Ok(Some(order))\n            }\n            Err(e) => anyhow::bail!(\"Failed to load order events: {e}\"),\n        }\n    }\n\n    /// Loads and assembles all `OrderAny` entries via the provided `pool`.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if loading events or SQL operations fail.\n    pub async fn load_orders(pool: &PgPool) -> anyhow::Result<Vec<OrderAny>> {\n        let mut orders: Vec<OrderAny> = Vec::new();\n        let client_order_ids: Vec<ClientOrderId> = sqlx::query(\n            r#\"\n            SELECT DISTINCT client_order_id FROM \"order_event\"\n        \"#,\n        )\n        .fetch_all(pool)\n        .await\n        .map(|rows| {","sourceCodeStart":787,"sourceCodeEnd":823,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L787-L823","documentation":"load_order fetches all events for a client_order_id and then assembles an OrderAny from them. When the underlying database query for the order events itself returns an error, the function bails with this message wrapping the query error; note it is distinct from the separate 'Failed to assemble order' error which fires when the query succeeded but event reconstruction failed.","triggerScenarios":"Calling load_order (or load orders via pool) when the SELECT of order_events rows fails: connection pool exhaustion, the database is unreachable, the table does not exist (schema not initialized), or a Postgres error occurs during the query execution.","commonSituations":"Running queries before init_postgres created the schema tables; network/credential problems between the client and Postgres; connection pool timeouts under load; pointing the client at the wrong database name.","solutions":["Inspect the wrapped error {e} in the message for the SQLSTATE/driver cause and address it (connectivity, credentials, permissions)","Run init_postgres / schema initialization first if the order_events table does not exist in the target database","Verify pool configuration (max connections, timeouts) and database reachability; retry transient connection failures","Confirm the connection points at the intended database where orders were persisted"],"exampleFix":"// before\nlet order = cache.load_order(&client_order_id)?; // bails if DB unreachable\n// after\nmatch cache.load_order(&client_order_id) {\n    Ok(o) => o,\n    Err(e) if e.to_string().contains(\"DatabaseError\") => {\n        tracing::error!(\"order event query failed: {e}\");\n        return Err(e);\n    }\n    Err(e) => return Err(e),\n}","handlingStrategy":"retry","validationCode":"// pre-check table exists before loading\nSELECT to_regclass('public.order_events') IS NOT NULL;","typeGuard":null,"tryCatchPattern":"match cache.load_order(&client_order_id).await {\n    Ok(Some(o)) => o,\n    Ok(None) => return Err(anyhow::anyhow!(\"order not found\")),\n    Err(e) if e.to_string().contains(\"Failed to load order events\") => {\n        // transient DB issue: backoff and retry\n        tokio::time::sleep(Duration::from_millis(200)).await;\n        cache.load_order(&client_order_id).await?\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Initialize the schema (init_postgres) before any load calls","Configure the PgPool with sane max_size/acquire timeouts and keep the database reachable","Distinguish this query-failure error from the 'Failed to assemble order' error when triaging logs"],"tags":["database","postgres","persistence","order-loading"],"backgroundTag":"database-query-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}