{"record":{"id":"74d087f80afb4123","repo":"nautechsystems/nautilus_trader","slug":"failed-to-assemble-order-client-order-id-from-ev","errorCode":null,"errorMessage":"Failed to assemble order {client_order_id} from events: {e}","messagePattern":"Failed to assemble order (.+?) from events: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":801,"sourceCode":"\n    /// Loads and assembles a complete `OrderAny` for a `client_order_id` via the provided `pool`.\n    ///\n    /// # Errors\n    ///\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        \"#,","sourceCodeStart":783,"sourceCodeEnd":819,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L783-L819","documentation":"load_order fetched order event rows from the database but OrderAny::from_events could not rebuild an Order from the event stream. The underlying deserialization/reassembly error is wrapped in this message with the client_order_id for context. It indicates the persisted event log for that order is corrupt, incomplete, or from an incompatible schema/version.","triggerScenarios":"Calling DatabaseQuery::load_order(pool, &client_order_id) where the rows for the order deserialize fine individually but OrderAny::from_events fails, e.g. first event is not an OrderInitialized, events are out of order, or an event field fails serde deserialization.","commonSituations":"Events written by an older nautilus version whose event schema changed (renamed/retyped JSON fields); manually edited or partially deleted order_event rows; events for one order mixed with another due to a wrong client_order_id index; truncated inserts from a previously failed transaction.","solutions":["Inspect the wrapped inner error in the message to identify the exact failing event and field.","Dump the order_event rows for that client_order_id and verify the first event is an OrderInitialized and ordering by ts_init/event_id is correct.","Check whether the rows were written by a different nautilus version; re-import or migrate the data to the current event schema.","Delete/re-load the affected order from the original source of truth, or remove the corrupt rows if the order can be discarded."],"exampleFix":"// before\nlet order = DatabaseQuery::load_order(&pool, &client_order_id).await?;\n// after\nmatch DatabaseQuery::load_order(&pool, &client_order_id).await {\n    Ok(order) => order,\n    Err(e) if e.to_string().contains(\"Failed to assemble order\") => {\n        tracing::error!(\"Corrupt event log for {client_order_id}: {e:#}\");\n        None // skip or re-ingest this order\n    }\n    Err(e) => return Err(e),\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"fn is_assembly_error(e: &anyhow::Error) -> bool {\n    e.to_string().contains(\"Failed to assemble order\")\n}","tryCatchPattern":"match load_order(pool, &client_order_id).await {\n    Ok(order) => order,\n    Err(e) if is_assembly_error(&e) => {\n        tracing::error!(\"order event log corrupt: {e:#}\");\n        None\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Never edit or delete individual order_event rows by hand.","Pin read/write paths to the same nautilus version and run migrations on upgrades.","Alert on from_events failures — they indicate corrupt or mismatched event logs."],"tags":["rust","database","event-sourcing","deserialization","anyhow"],"backgroundTag":"event-replay-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}