Skip to content

Adopt structured error type #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde = { version = "1.0", default-features = false, features = ["alloc", "deriv
const_format = "0.2.34"
futures-lite = { version = "2.6.0", default-features = false, features = ["alloc"] }
rustc-hash = { version = "2.1", default-features = false }
thiserror = { version = "2", default-features = false }

[dependencies.uuid]
version = "1.4.1"
Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/bson/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ impl BsonError {
}
}

impl core::error::Error for BsonError {}

impl Display for BsonError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.err.fmt(f)
Expand Down
9 changes: 5 additions & 4 deletions crates/core/src/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use sqlite_nostd as sqlite;
use sqlite_nostd::{Connection, Context, Value};

use crate::create_sqlite_text_fn;
use crate::error::SQLiteError;
use crate::error::PowerSyncError;
use crate::sync::checkpoint::{validate_checkpoint, OwnedBucketChecksum};
use crate::sync::line::Checkpoint;

Expand All @@ -24,9 +24,10 @@ struct CheckpointResult {
fn powersync_validate_checkpoint_impl(
ctx: *mut sqlite::context,
args: &[*mut sqlite::value],
) -> Result<String, SQLiteError> {
) -> Result<String, PowerSyncError> {
let data = args[0].text();
let checkpoint: Checkpoint = serde_json::from_str(data)?;
let checkpoint: Checkpoint =
serde_json::from_str(data).map_err(PowerSyncError::as_argument_error)?;
let db = ctx.db_handle();
let buckets: Vec<OwnedBucketChecksum> = checkpoint
.buckets
Expand All @@ -45,7 +46,7 @@ fn powersync_validate_checkpoint_impl(
failed_buckets: failed_buckets,
};

Ok(json::to_string(&result)?)
Ok(json::to_string(&result).map_err(PowerSyncError::internal)?)
}

create_sqlite_text_fn!(
Expand Down
13 changes: 7 additions & 6 deletions crates/core/src/crud_vtab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sqlite::{Connection, ResultCode, Value};
use sqlite_nostd::ManagedStmt;
use sqlite_nostd::{self as sqlite, ColumnType};

use crate::error::SQLiteError;
use crate::error::PowerSyncError;
use crate::ext::SafeManagedStmt;
use crate::schema::TableInfoFlags;
use crate::state::DatabaseState;
Expand Down Expand Up @@ -81,11 +81,11 @@ impl VirtualTable {
}
}

fn handle_insert(&mut self, args: &[*mut sqlite::value]) -> Result<(), SQLiteError> {
fn handle_insert(&mut self, args: &[*mut sqlite::value]) -> Result<(), PowerSyncError> {
let current_tx = self
.current_tx
.as_mut()
.ok_or_else(|| SQLiteError::misuse("No tx_id"))?;
.ok_or_else(|| PowerSyncError::state_error("Not in tx"))?;
let db = self.db;

if self.state.is_in_sync_local.load(Ordering::Relaxed) {
Expand Down Expand Up @@ -162,7 +162,8 @@ impl VirtualTable {
} else {
None
},
})?;
})
.map_err(PowerSyncError::internal)?;
stmt.bind_text(2, &serialized, sqlite::Destructor::STATIC)?;
stmt.exec()?;

Expand All @@ -178,7 +179,7 @@ impl VirtualTable {
Ok(())
}

fn begin(&mut self) -> Result<(), SQLiteError> {
fn begin(&mut self) -> Result<(), PowerSyncError> {
let db = self.db;

// language=SQLite
Expand All @@ -187,7 +188,7 @@ impl VirtualTable {
let tx_id = if statement.step()? == ResultCode::ROW {
statement.column_int64(0) - 1
} else {
return Err(SQLiteError::from(ResultCode::ABORT));
return Err(PowerSyncError::unknown_internal());
};

self.current_tx = Some(ActiveCrudTransaction {
Expand Down
12 changes: 6 additions & 6 deletions crates/core/src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ use sqlite_nostd::{Connection, Context, Value};
use serde_json as json;

use crate::create_sqlite_text_fn;
use crate::error::SQLiteError;
use crate::error::PowerSyncError;

fn powersync_diff_impl(
_ctx: *mut sqlite::context,
args: &[*mut sqlite::value],
) -> Result<String, SQLiteError> {
) -> Result<String, PowerSyncError> {
let data_old = args[0].text();
let data_new = args[1].text();

diff_objects(data_old, data_new)
}

pub fn diff_objects(data_old: &str, data_new: &str) -> Result<String, SQLiteError> {
let v_new: json::Value = json::from_str(data_new)?;
let v_old: json::Value = json::from_str(data_old)?;
pub fn diff_objects(data_old: &str, data_new: &str) -> Result<String, PowerSyncError> {
let v_new: json::Value = json::from_str(data_new).map_err(PowerSyncError::as_argument_error)?;
let v_old: json::Value = json::from_str(data_old).map_err(PowerSyncError::as_argument_error)?;

if let (json::Value::Object(mut left), json::Value::Object(mut right)) = (v_new, v_old) {
// Remove all null values
Expand Down Expand Up @@ -56,7 +56,7 @@ pub fn diff_objects(data_old: &str, data_new: &str) -> Result<String, SQLiteErro

Ok(json::Value::Object(left).to_string())
} else {
Err(SQLiteError::from(ResultCode::MISMATCH))
return Err(PowerSyncError::argument_error("expected two JSON objects"));
}
}

Expand Down
Loading