@@ -13,7 +13,8 @@ use crate::bson::BsonError;
1313
1414/// A [RawPowerSyncError], but boxed.
1515///
16- /// We allocate errors in boxes to avoid large [Result] types returning these.
16+ /// We allocate errors in boxes to avoid large [Result] types (given the large size of the
17+ /// [RawPowerSyncError] enum type).
1718pub struct PowerSyncError {
1819 inner : Box < RawPowerSyncError > ,
1920}
@@ -35,10 +36,14 @@ impl PowerSyncError {
3536 . into ( )
3637 }
3738
38- pub fn json_argument_error ( cause : serde_json:: Error ) -> Self {
39+ /// Converts something that can be a [PowerSyncErrorCause] into an argument error.
40+ ///
41+ /// This can be used to represent e.g. JSON parsing errors as argument errors, e.g. with
42+ /// ` serde_json::from_str(payload.text()).map_err(PowerSyncError::as_argument_error)`.
43+ pub fn as_argument_error ( cause : impl Into < PowerSyncErrorCause > ) -> Self {
3944 RawPowerSyncError :: ArgumentError {
4045 desc : "" . into ( ) ,
41- cause : PowerSyncErrorCause :: Json ( cause) ,
46+ cause : cause. into ( ) ,
4247 }
4348 . into ( )
4449 }
@@ -54,17 +59,35 @@ impl PowerSyncError {
5459 RawPowerSyncError :: StateError { desc } . into ( )
5560 }
5661
62+ pub fn sync_protocol_error ( desc : & ' static str , cause : impl Into < PowerSyncErrorCause > ) -> Self {
63+ RawPowerSyncError :: SyncProtocolError {
64+ desc,
65+ cause : cause. into ( ) ,
66+ }
67+ . into ( )
68+ }
69+
70+ /// A generic internal error.
71+ ///
72+ /// This should only be used rarely since this error provides no further details.
5773 pub fn unknown_internal ( ) -> Self {
5874 Self :: internal ( PowerSyncErrorCause :: Unknown )
5975 }
6076
77+ /// A generic internal error with an associated cause.
6178 pub fn internal ( cause : impl Into < PowerSyncErrorCause > ) -> Self {
6279 RawPowerSyncError :: Internal {
6380 cause : cause. into ( ) ,
6481 }
6582 . into ( )
6683 }
6784
85+ pub fn missing_client_id ( ) -> Self {
86+ RawPowerSyncError :: MissingClientId . into ( )
87+ }
88+
89+ /// Applies this error to a function result context, setting the error code and a descriptive
90+ /// text.
6891 pub fn apply_to_ctx ( self , description : & str , ctx : * mut context ) {
6992 let mut desc = self . description ( ctx. db_handle ( ) ) ;
7093 desc. insert_str ( 0 , description) ;
@@ -91,7 +114,7 @@ impl PowerSyncError {
91114
92115 match self . inner . as_ref ( ) {
93116 Sqlite { code, .. } => * code,
94- InvalidBucketPriority | ArgumentError { .. } | StateError { .. } => ResultCode :: MISUSE ,
117+ ArgumentError { .. } | StateError { .. } => ResultCode :: MISUSE ,
95118 MissingClientId | SyncProtocolError { .. } => ResultCode :: ABORT ,
96119 LocalDataError { .. } => ResultCode :: CORRUPT ,
97120 Internal { .. } => ResultCode :: INTERNAL ,
@@ -125,7 +148,7 @@ impl From<ResultCode> for PowerSyncError {
125148
126149/// A structured enumeration of possible errors that can occur in the core extension.
127150#[ derive( Error , Debug ) ]
128- pub enum RawPowerSyncError {
151+ enum RawPowerSyncError {
129152 /// An internal call to SQLite made by the core extension has failed. We store the original
130153 /// result code and an optional context describing what the core extension was trying to do when
131154 /// the error occurred.
@@ -158,19 +181,18 @@ pub enum RawPowerSyncError {
158181 /// (e.g. a checkpoint diff before we've ever received a checkpoint).
159182 ///
160183 /// This interrupts a sync iteration as we cannot reasonably continue afterwards (the client and
161- /// server are necessarily in different states).
184+ /// server are necessarily in diverged states).
162185 #[ error( "Sync protocol error: {desc}. {cause}" ) ]
163186 SyncProtocolError {
164187 desc : & ' static str ,
165188 cause : PowerSyncErrorCause ,
166189 } ,
167190 /// There's invalid local data in the database (like malformed JSON in the oplog table).
168- #[ error( "invalid local data" ) ]
191+ #[ error( "invalid local data: {cause} " ) ]
169192 LocalDataError { cause : PowerSyncErrorCause } ,
170193 #[ error( "No client_id found in ps_kv" ) ]
171194 MissingClientId ,
172- #[ error( "Invalid bucket priority value" ) ]
173- InvalidBucketPriority ,
195+ /// A catch-all for remaining internal errors that are very unlikely to happen.
174196 #[ error( "Internal PowerSync error. {cause}" ) ]
175197 Internal { cause : PowerSyncErrorCause } ,
176198}
0 commit comments