{"record":{"id":"c665398e5afb5d79","repo":"nautechsystems/nautilus_trader","slug":"failed-to-load-tokens-e","errorCode":null,"errorMessage":"Failed to load tokens: {e}","messagePattern":"Failed to load tokens: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/cache/database.rs","lineNumber":1318,"sourceCode":"    pub async fn load_tokens(&self, chain: SharedChain) -> anyhow::Result<Vec<Token>> {\n        sqlx::query_as::<_, TokenRow>(\"SELECT * FROM token WHERE chain_id = $1 AND error IS NULL\")\n            .bind(chain.chain_id as i32)\n            .fetch_all(&self.pool)\n            .await\n            .map(|rows| {\n                rows.into_iter()\n                    .map(|token_row| {\n                        Token::new(\n                            chain.clone(),\n                            token_row.address,\n                            token_row.name,\n                            token_row.symbol,\n                            token_row.decimals,\n                        )\n                    })\n                    .collect::<Vec<_>>()\n            })\n            .map_err(|e| anyhow::anyhow!(\"Failed to load tokens: {e}\"))\n    }\n\n    /// Retrieves all invalid token addresses for a given chain.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the database query fails or address validation fails.\n    pub async fn load_invalid_token_addresses(\n        &self,\n        chain_id: u32,\n    ) -> anyhow::Result<Vec<Address>> {\n        sqlx::query_as::<_, (String,)>(\n            \"SELECT address FROM token WHERE chain_id = $1 AND error IS NOT NULL\",\n        )\n        .bind(chain_id as i32)\n        .fetch_all(&self.pool)\n        .await?\n        .into_iter()","sourceCodeStart":1300,"sourceCodeEnd":1336,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/cache/database.rs#L1300-L1336","documentation":"Wraps failures while fetching and mapping all valid token rows for a chain into `Token` domain objects. Both the SQL fetch and the per-row conversion can fail; whichever errors is surfaced through this message. Invalid tokens (rows with error info) are deliberately excluded from the result.","triggerScenarios":"Calling the token-loading method (database.rs, load tokens for chain) when the SELECT fails (connection, missing table/column) or when row-to-Token conversion fails, e.g. a stored symbol/decimals row that cannot be parsed into the domain type.","commonSituations":"Querying a chain_id with a corrupted token row (bad decimals value) that fails domain validation; running the app against an older DB schema lacking a column; pool connection dropped during fetch_all.","solutions":["Read the underlying `{e}` to see whether the failure is SQL-level or row-mapping level.","Remove or repair rows with unparseable decimals/symbol values from the `token` table.","Run migrations so the queried columns exist for the deployed schema.","Confirm the database is reachable and the pool is healthy before bulk loads."],"exampleFix":"// before: any bad row aborts the whole load\n.map_err(|e| anyhow::anyhow!(\"Failed to load tokens: {e}\"))\n// after: repair the offending row data instead\n// UPDATE token SET decimals = 18 WHERE chain_id = 1 AND decimals IS NULL;","handlingStrategy":"validation","validationCode":"// sanity-check stored token rows before bulk loading\nlet bad: (i64,) = sqlx::query_as(\n    \"SELECT count(*) FROM token WHERE chain_id = $1 AND (decimals IS NULL OR decimals < 0 OR symbol IS NULL)\",\n).bind(chain_id).fetch_one(&pool).await?;\nif bad.0 > 0 { log::warn!(\"{bad} token rows will fail domain conversion\"); }","typeGuard":null,"tryCatchPattern":"let tokens = match self.load_tokens(chain).await {\n    Ok(t) => t,\n    Err(e) if e.to_string().contains(\"decimals\") => {\n        log::error!(\"corrupt token row: {e}; repair token table before retry\");\n        Vec::new()\n    }\n    Err(e) => return Err(e),\n};","preventionTips":["Validate token decimals/symbol at write time so bad rows never enter the table.","Run migrations before app startup cache loads.","Schedule a periodic data-integrity query on the token table.","Keep an eye on fetch_all size; paginate very large token sets."],"tags":["database","sqlx","postgres","query-failed","data-mapping"],"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"}