From 44de6c49644d4e8d1c44f289aa84c91db7bd88f1 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Tue, 25 Apr 2023 11:04:06 -0700 Subject: [PATCH] Actually use the error handler set in EventConfiguration (#8217) --- CHANGELOG.md | 3 +-- Realm/ObjectServerTests/EventTests.swift | 34 +++++++++++++++++++----- Realm/RLMEvent.h | 2 +- RealmSwift/RealmConfiguration.swift | 4 ++- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 540cb938a3..1225daffe9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,7 @@ x.y.z Release notes (yyyy-MM-dd) * None. ### Fixed -* ([#????](https://github.com/realm/realm-swift/issues/????), since v?.?.?) -* None. +* The error handler set on EventsConfiguration was not actually used (since v10.26.0). diff --git a/Realm/ObjectServerTests/EventTests.swift b/Realm/ObjectServerTests/EventTests.swift index 78c7d027e6..594c50c73e 100644 --- a/Realm/ObjectServerTests/EventTests.swift +++ b/Realm/ObjectServerTests/EventTests.swift @@ -23,6 +23,7 @@ import XCTest #if canImport(RealmTestSupport) import RealmSwiftSyncTestSupport +import RealmSwiftTestSupport import RealmSyncTestSupport import RealmTestSupport #endif @@ -415,25 +416,25 @@ class SwiftEventTests: SwiftSyncTestCase { assertEvent("link via subscript", personCount: 1, linkAccessed) assertEvent("link via dynamic", personCount: 1, linkAccessed) - let listAccsssed: NSDictionary = [ + let listAccessed: NSDictionary = [ "_id": id!.stringValue, "realm_id": NSNull(), "person": idOnly(a), "people": [full(b), full(c)], "peopleByName": [b.firstName: idOnly(b), c.firstName: idOnly(c)] ] - assertEvent("list property", personCount: 1, listAccsssed) - assertEvent("dynamic list property", personCount: 1, listAccsssed) + assertEvent("list property", personCount: 1, listAccessed) + assertEvent("dynamic list property", personCount: 1, listAccessed) - let dictionaryAccsssed: NSDictionary = [ + let dictionaryAccessed: NSDictionary = [ "_id": id!.stringValue, "realm_id": NSNull(), "person": idOnly(a), "people": [idOnly(b), idOnly(c)], "peopleByName": [b.firstName: full(b), c.firstName: full(c)] ] - assertEvent("dictionary property", personCount: 1, dictionaryAccsssed) - assertEvent("dynamic dictionary property", personCount: 1, dictionaryAccsssed) + assertEvent("dictionary property", personCount: 1, dictionaryAccessed) + assertEvent("dynamic dictionary property", personCount: 1, dictionaryAccessed) } func testMetadata() throws { @@ -537,4 +538,25 @@ class SwiftEventTests: SwiftSyncTestCase { let result = getEvents(expectedCount: 1) XCTAssertEqual(result[0].activity, "scope") } + + func testErrorHandler() throws { + var config = try self.config() + let blockCalled = Locked(false) + let ex = expectation(description: "Error callback called") + config.eventConfiguration?.errorHandler = { error in + assertSyncError(error, .clientUserError, "Unable to refresh the user access token.") + blockCalled.value = true + ex.fulfill() + } + let realm = try openRealm(configuration: config) + let events = realm.events! + manuallySetAccessToken(for: user, value: badAccessToken()) + manuallySetRefreshToken(for: user, value: badAccessToken()) + + // Recording the audit event should succeed, but we should get a sync + // error when trying to actually upload it due to the user having + // an invalid access token + events.recordEvent(activity: "activity which should fail").await(self) + wait(for: [ex], timeout: 4.0) + } } diff --git a/Realm/RLMEvent.h b/Realm/RLMEvent.h index a0d4d707d6..85f76430f8 100644 --- a/Realm/RLMEvent.h +++ b/Realm/RLMEvent.h @@ -51,7 +51,7 @@ FOUNDATION_EXTERN void RLMEventUpdateMetadata(struct RLMEventContext *context, @property (nonatomic, nullable) RLMUser *syncUser; @property (nonatomic, nullable) NSDictionary *metadata; @property (nonatomic, nullable) void (^logger)(RLMSyncLogLevel, NSString *); -@property (nonatomic, nullable) void (^errorHandler)(NSError *); +@property (nonatomic, nullable) RLM_SWIFT_SENDABLE void (^errorHandler)(NSError *); #ifdef __cplusplus - (std::shared_ptr)auditConfigWithRealmConfiguration:(RLMRealmConfiguration *)realmConfig; diff --git a/RealmSwift/RealmConfiguration.swift b/RealmSwift/RealmConfiguration.swift index 25dfa83569..d99d1edf65 100644 --- a/RealmSwift/RealmConfiguration.swift +++ b/RealmSwift/RealmConfiguration.swift @@ -310,6 +310,7 @@ extension Realm { rlmConfig.syncUser = eventConfiguration.syncUser rlmConfig.metadata = eventConfiguration.metadata rlmConfig.logger = eventConfiguration.logger + rlmConfig.errorHandler = eventConfiguration.errorHandler configuration.eventConfiguration = rlmConfig } @@ -338,7 +339,8 @@ extension Realm { if let eventConfiguration = rlmConfiguration.eventConfiguration { configuration.eventConfiguration = EventConfiguration(metadata: eventConfiguration.metadata, syncUser: eventConfiguration.syncUser, - partitionPrefix: eventConfiguration.partitionPrefix) + partitionPrefix: eventConfiguration.partitionPrefix, + errorHandler: eventConfiguration.errorHandler) } configuration.initialSubscriptions = ObjectiveCSupport.convert(block: rlmConfiguration.initialSubscriptions)