diff --git a/CHANGELOG.md b/CHANGELOG.md index db16171b4..1f89cf698 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,13 +9,13 @@ * Support setting `maxNumberOfActiveVersions` when creating a `Configuration`. ([#1036](https://github.com/realm/realm-dart/pull/1036)) ### Fixed -* None +* Support mapping into `SyncSessionErrorCode` for "Compensating write" with error code 231. ([#1022](https://github.com/realm/realm-dart/pull/1022)) ### Compatibility * Realm Studio: 12.0.0 or later. ### Internal -* Using Core x.y.z. +* Using Core 12.12.0. ## 0.8.0+rc (2022-11-14) diff --git a/common/lib/src/realm_types.dart b/common/lib/src/realm_types.dart index 0f60d47c0..07435e0d8 100644 --- a/common/lib/src/realm_types.dart +++ b/common/lib/src/realm_types.dart @@ -424,8 +424,13 @@ enum SyncSessionErrorCode { /// Client tried to open a session before initial sync is complete (BIND) initialSyncNotCompleted(229), - /// Client attempted a write that is disallowed by permissions, or modifies an object outside the current query - requires client reset (UPLOAD) - writeNotAllowed(230); + /// Client attempted a write that is disallowed by permissions, or modifies an object + /// outside the current query - requires client reset (UPLOAD) + writeNotAllowed(230), + + /// Client attempted a write that is disallowed by permissions, or modifies an object + /// outside the current query, and the server undid the modification (UPLOAD) + compensatingWrite(231); static final Map _valuesMap = {for (var value in SyncSessionErrorCode.values) value.code: value}; diff --git a/test/subscription_test.dart b/test/subscription_test.dart index b0cf31bb2..f1f55c672 100644 --- a/test/subscription_test.dart +++ b/test/subscription_test.dart @@ -27,6 +27,7 @@ import '../lib/realm.dart'; import '../lib/src/configuration.dart'; import '../lib/src/native/realm_core.dart'; import '../lib/src/subscription.dart'; +import 'client_reset_test.dart'; import 'test.dart'; @isTest @@ -605,4 +606,29 @@ Future main([List? args]) async { realm.close(); expect(() => subscriptions.state, throws()); }); + + baasTest('SyncSessionErrorCode.compensatingWrite', (configuration) async { + late SyncError compensatingWriteError; + final productNamePrefix = generateRandomString(4); + final app = App(configuration); + final user = await getIntegrationUser(app); + final config = Configuration.flexibleSync(user, [Product.schema], syncErrorHandler: (syncError) { + compensatingWriteError = syncError; + }); + final realm = getRealm(config); + final query = realm.query(r'stringQueryField BEGINSWITH $0', [productNamePrefix]); + if (realm.subscriptions.find(query) == null) { + realm.subscriptions.update((mutableSubscriptions) => mutableSubscriptions.add(query)); + } + await realm.subscriptions.waitForSynchronization(); + realm.write(() => realm.add(Product(ObjectId(), "doesn't match subscription"))); + await realm.syncSession.waitForUpload(); + + expect(compensatingWriteError, isA()); + final sessionError = compensatingWriteError.as(); + expect(sessionError.category, SyncErrorCategory.session); + expect(sessionError.isFatal, false); + expect(sessionError.code, SyncSessionErrorCode.compensatingWrite); + expect(sessionError.message!.startsWith('Client attempted a write that is outside of permissions or query filters'), isTrue); + }); }