Adopt structured error type #96
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a preparation step to improve error handling in the sync client - basically, we'll want some operations to be "retryable" - e.g. if we reach a "database locked" error during
sync_local
, there's no reason to shut the entire sync iteration down. A client could just retry after unlocking the database.Other errors, like e.g. a decoding error for sync lines or a state mismatch (like receiving a
checkpoint_diff
without acheckpoint
line firs) are still fatal though.To reliably determine which errors interrupt a sync iteration and which don't, this begins by refactoring the error handling in
core
:SQLiteError
has been renamed toPowerSyncError
, because we're not just dealing with SQLite errors after all.PowerSyncError
defines an enum of all possible errors in the core extension. We can still "rethrow" SQLite errors (and also add context if necessary), but other issues like e.g. problems decoding JSON now have their own enum entry depending on where they occur (likeArgumentError
orSyncProtocolError
).thiserror
crate to simplify defining this error type - the messages returned by our SQLite functions have also been improved somewhat.As a follow-up, #97 will change the sync client to use the structured errors to determine which are fatal and which can be retried. We could technically do the same thing with result codes alone, but it's still nice to be explicit about which error types are recoverable.