From 61f0a4b05ede498dbbdfd66c47810841086fd979 Mon Sep 17 00:00:00 2001 From: Nasr Date: Wed, 2 Oct 2024 15:51:35 -0400 Subject: [PATCH] refactor: handle errors without panic --- crates/torii/core/src/sql.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/crates/torii/core/src/sql.rs b/crates/torii/core/src/sql.rs index 64899fae59..c42e447080 100644 --- a/crates/torii/core/src/sql.rs +++ b/crates/torii/core/src/sql.rs @@ -74,20 +74,29 @@ impl Sql { let indexer: (Option, Option, Option, String) = indexer_query.fetch_one(&self.pool).await?; + Ok(( - indexer.0.map(|h| h.try_into().expect("doesn't fit in u64")).unwrap_or(0), + indexer + .0 + .map(|h| h.try_into().map_err(|_| anyhow!("Head value {} doesn't fit in u64", h))) + .transpose()? + .unwrap_or(0), indexer.1.map(|f| Felt::from_str(&f)).transpose()?, indexer.2.map(|f| Felt::from_str(&f)).transpose()?, )) } pub fn set_head(&mut self, head: u64) -> Result<()> { - let head = Argument::Int(head.try_into().expect("doesn't fit in u64")); + let head = Argument::Int( + head.try_into().map_err(|_| anyhow!("Head value {} doesn't fit in i64", head))?, + ); let id = Argument::FieldElement(self.world_address); - self.executor.send(QueryMessage::other( - "UPDATE contracts SET head = ? WHERE id = ?".to_string(), - vec![head, id], - ))?; + self.executor + .send(QueryMessage::other( + "UPDATE contracts SET head = ? WHERE id = ?".to_string(), + vec![head, id], + )) + .map_err(|e| anyhow!("Failed to send set_head message: {}", e))?; Ok(()) }