Skip to content

Commit

Permalink
Actually use the error handler set in EventConfiguration (#8217)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne authored Apr 25, 2023
1 parent 4386694 commit 44de6c4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ x.y.z Release notes (yyyy-MM-dd)
* None.

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-swift/issues/????), since v?.?.?)
* None.
* The error handler set on EventsConfiguration was not actually used (since v10.26.0).

<!-- ### Breaking Changes - ONLY INCLUDE FOR NEW MAJOR version -->

Expand Down
34 changes: 28 additions & 6 deletions Realm/ObjectServerTests/EventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import XCTest

#if canImport(RealmTestSupport)
import RealmSwiftSyncTestSupport
import RealmSwiftTestSupport
import RealmSyncTestSupport
import RealmTestSupport
#endif
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion Realm/RLMEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ FOUNDATION_EXTERN void RLMEventUpdateMetadata(struct RLMEventContext *context,
@property (nonatomic, nullable) RLMUser *syncUser;
@property (nonatomic, nullable) NSDictionary<NSString *, NSString *> *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<realm::AuditConfig>)auditConfigWithRealmConfiguration:(RLMRealmConfiguration *)realmConfig;
Expand Down
4 changes: 3 additions & 1 deletion RealmSwift/RealmConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 44de6c4

Please sign in to comment.