Skip to content

Commit

Permalink
make API call errors convertible to String (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril authored and cloutiertyler committed Aug 1, 2023
1 parent 0d0b97c commit 3da3402
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
5 changes: 5 additions & 0 deletions crates/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ impl<T: TableType> fmt::Display for UniqueConstraintViolation<T> {
)
}
}
impl<T: TableType> From<UniqueConstraintViolation<T>> for String {
fn from(err: UniqueConstraintViolation<T>) -> Self {
err.to_string()
}
}
impl<T: TableType> std::error::Error for UniqueConstraintViolation<T> {}

impl<T: TableType> sealed::InsertResult for Result<T, UniqueConstraintViolation<T>> {
Expand Down
6 changes: 5 additions & 1 deletion crates/sats/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ impl fmt::Display for DecodeError {
}
}
}

impl From<DecodeError> for String {
fn from(err: DecodeError) -> Self {
err.to_string()
}
}
impl std::error::Error for DecodeError {}

impl From<Utf8Error> for DecodeError {
Expand Down
17 changes: 4 additions & 13 deletions modules/rust-wasm-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,15 @@ pub fn test(ctx: ReducerContext, arg: TestA, arg2: TestB, arg3: TestC) -> anyhow
});
}

let mut row_count = 0;
for _row in TestA::iter() {
row_count += 1;
}
let row_count = TestA::iter().count();

log::info!("Row count before delete: {:?}", row_count);

for row in 5..10 {
delete_eq(1, 0, &AlgebraicValue::U32(row))?;
}

let mut row_count = 0;
for _row in TestA::iter() {
row_count += 1;
}
let row_count = TestA::iter().count();

match TestE::insert(TestE {
id: 0,
Expand All @@ -106,10 +100,7 @@ pub fn test(ctx: ReducerContext, arg: TestA, arg2: TestB, arg3: TestC) -> anyhow

log::info!("Row count after delete: {:?}", row_count);

let mut other_row_count = 0;
for _row in query!(|row: TestA| row.x >= 0 && row.x <= u32::MAX) {
other_row_count += 1;
}
let other_row_count = query!(|row: TestA| row.x >= 0 && row.x <= u32::MAX).count();

log::info!("Row count filtered by condition: {:?}", other_row_count);

Expand All @@ -119,7 +110,7 @@ pub fn test(ctx: ReducerContext, arg: TestA, arg2: TestB, arg3: TestC) -> anyhow

#[spacetimedb(reducer)]
pub fn add_player(name: String) -> Result<(), String> {
TestE::insert(TestE { id: 0, name }).map_err(|_| "Duplicate row entry.".to_string())?;
TestE::insert(TestE { id: 0, name })?;
Ok(())
}

Expand Down

0 comments on commit 3da3402

Please sign in to comment.