Skip to content

Commit

Permalink
refactor: handle errors without panic
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Oct 2, 2024
1 parent 6b6f5a6 commit 61f0a4b
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions crates/torii/core/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,29 @@ impl Sql {

let indexer: (Option<i64>, Option<String>, Option<String>, 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(())
}
Expand Down

0 comments on commit 61f0a4b

Please sign in to comment.