{"record":{"id":"924b51f152bb6f75","repo":"nautechsystems/nautilus_trader","slug":"failed-to-serialize-account-event-e","errorCode":null,"errorMessage":"Failed to serialize account event: {e}","messagePattern":"Failed to serialize account event: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/infrastructure/src/sql/queries.rs","lineNumber":1110,"sourceCode":"        pool: &PgPool,\n        updated: bool,\n        account_event: AccountState,\n    ) -> anyhow::Result<()> {\n        if updated {\n            let exists =\n                Self::check_if_account_event_exists(pool, account_event.account_id).await?;\n\n            if !exists {\n                anyhow::bail!(\n                    \"Account event does not exist for account: {}\",\n                    account_event.account_id\n                );\n            }\n        }\n\n        let mut transaction = pool.begin().await?;\n        let event = serde_json::to_value(&account_event)\n            .map_err(|e| anyhow::anyhow!(\"Failed to serialize account event: {e}\"))?;\n        let balances = event\n            .get(\"balances\")\n            .cloned()\n            .ok_or_else(|| anyhow::anyhow!(\"Serialized account event has no balances\"))?;\n        let margins = event\n            .get(\"margins\")\n            .cloned()\n            .ok_or_else(|| anyhow::anyhow!(\"Serialized account event has no margins\"))?;\n\n        sqlx::query(\n            r#\"\n            INSERT INTO \"account\" (id) VALUES ($1) ON CONFLICT (id) DO NOTHING\n        \"#,\n        )\n        .bind(account_event.account_id.to_string())\n        .execute(&mut *transaction)\n        .await\n        .map(|_| ())","sourceCodeStart":1092,"sourceCodeEnd":1128,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/infrastructure/src/sql/queries.rs#L1092-L1128","documentation":"add_account serializes the whole AccountState event to serde_json::Value and then picks out the \"balances\" and \"margins\" keys. This error means serde_json::to_value(&account_event) itself returned Err. Since AccountState derives Serialize, this is rare and typically indicates a custom or poisoned serialization path (e.g. an f64 NaN/Infinity in an override, or a broken custom Serialize impl for a contained type).","triggerScenarios":"Calling add_account (or the higher-level cache write_account/adapter) with an AccountState whose serialization to JSON fails — custom Serialize implementations returning Err, or serde_json configured in a mode where non-finite floats hard-error.","commonSituations":"A custom account event type or Decimal-like type whose Serialize impl can fail; NaN or infinite balance values flowing through a serde_json build that rejects them; mixing serde_json versions across the dependency graph.","solutions":["Read the embedded serde error message to identify the offending field/type in the AccountState.","Sanitize balances/margins before constructing the AccountState: reject or replace NaN/infinite values.","Fix or replace the custom Serialize impl that returns Err.","Align serde_json versions in the dependency tree (cargo tree -i serde_json) if the failure stems from version mismatch."],"exampleFix":"// before: NaN balances blow up during serialization\nlet state = AccountState { balances: vec![balance_with(nan_price)], .. };\n\n// after: validate before persisting\nassert!(state.balances.iter().all(|b| b.total.is_finite()), \"non-finite balance\");\nadd_account(&pool, updated, state).await?;","handlingStrategy":"validation","validationCode":"// dry-run serialization before calling add_account\nserde_json::to_value(&account_event)\n    .expect(\"AccountState must serialize: check for NaN/Inf balances or bad custom Serialize impl\");","typeGuard":"fn is_serializable_account(state: &AccountState) -> bool {\n    serde_json::to_value(state).is_ok()\n}","tryCatchPattern":"let json = serde_json::to_value(&account_event)\n    .map_err(|e| { log::error!(\"account serialization failed: {e}\"); e })?;","preventionTips":["Reject non-finite balance/margin values when constructing AccountState.","Keep serde_json versions unified across the dependency tree.","Add a round-trip (serialize then deserialize) test for AccountState.","Avoid custom Serialize impls on types embedded in account events."],"tags":["serialization","serde","json","account"],"backgroundTag":"json-marshal-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"}