Skip to content

Commit

Permalink
SyncSessionErrorCode.compensatingWrite(231) (#1022)
Browse files Browse the repository at this point in the history
* Add compensatingWrite error to SyncSessionErrorCode
* Test compensatingWrite
Fixes #1024
  • Loading branch information
desistefanova authored Nov 15, 2022
1 parent 82b28f8 commit 7f4331e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 7 additions & 2 deletions common/lib/src/realm_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, SyncSessionErrorCode> _valuesMap = {for (var value in SyncSessionErrorCode.values) value.code: value};

Expand Down
26 changes: 26 additions & 0 deletions test/subscription_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -605,4 +606,29 @@ Future<void> main([List<String>? args]) async {
realm.close();
expect(() => subscriptions.state, throws<RealmClosedError>());
});

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<Product>(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<SyncSessionError>());
final sessionError = compensatingWriteError.as<SyncSessionError>();
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);
});
}

0 comments on commit 7f4331e

Please sign in to comment.