Skip to content

Commit

Permalink
Merge pull request realm#4580 from realm/do-sync-error-codes
Browse files Browse the repository at this point in the history
Use sync error domain errors from network client
  • Loading branch information
stel authored Feb 20, 2017
2 parents 24bca85 + 2f9d6e5 commit 5a89982
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
29 changes: 29 additions & 0 deletions Realm/ObjectServerTests/RLMObjectServerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import "RLMSyncTestCase.h"
#import "RLMSyncSessionRefreshHandle+ObjectServerTests.h"
#import "RLMSyncUser+ObjectServerTests.h"
#import "RLMSyncUtil_Private.h"

#define ACCOUNT_NAME() NSStringFromSelector(_cmd)
#define CUSTOM_REALM_URL(realm_identifier) \
Expand Down Expand Up @@ -124,6 +125,34 @@ - (void)testExistingUsernameRegistration {
[self waitForExpectationsWithTimeout:2.0 handler:nil];
}

/// Errors reported in RLMSyncManager.errorHandler shouldn't contain sync error domain errors as underlying error
- (void)testSyncErrorHandlerErrorDomain {
RLMSyncUser *user = [self logInUserForCredentials:[RLMObjectServerTests basicCredentialsWithName:ACCOUNT_NAME()
register:YES]
server:[RLMObjectServerTests authServerURL]];
XCTAssertNotNil(user);

NSURL *realmURL = [NSURL URLWithString:@"realm://localhost:9080/THE_PATH_USER_DONT_HAVE_ACCESS_TO/test"];

RLMRealmConfiguration *c = [RLMRealmConfiguration defaultConfiguration];
c.syncConfiguration = [[RLMSyncConfiguration alloc] initWithUser:user realmURL:realmURL];

NSError *error = nil;
__attribute__((objc_precise_lifetime)) RLMRealm *realm = [RLMRealm realmWithConfiguration:c error:&error];
XCTAssertNil(error);
XCTAssertTrue(realm.isEmpty);

XCTestExpectation *expectation = [self expectationWithDescription:@""];
[RLMSyncManager sharedManager].errorHandler = ^(__unused NSError *error,
__unused RLMSyncSession *session) {
XCTAssertTrue([error.domain isEqualToString:RLMSyncErrorDomain]);
XCTAssertFalse([[error.userInfo[kRLMSyncUnderlyingErrorKey] domain] isEqualToString:RLMSyncErrorDomain]);
[expectation fulfill];
};

[self waitForExpectationsWithTimeout:2.0 handler:nil];
}

/// The pre-emptive token refresh subsystem should function, and properly refresh the token.
- (void)testPreemptiveTokenRefresh {
// Prepare the test.
Expand Down
2 changes: 1 addition & 1 deletion Realm/ObjectStore
14 changes: 11 additions & 3 deletions Realm/RLMSyncSessionRefreshHandle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,17 @@ - (BOOL)_handleSuccessfulRequest:(RLMAuthResponseModel *)model strongUser:(RLMSy

/// Handler for network requests that failed before the JSON parsing stage.
- (BOOL)_handleFailedRequest:(NSError *)error strongUser:(RLMSyncUser *)user {
NSError *syncError = [NSError errorWithDomain:RLMSyncErrorDomain
code:RLMSyncErrorBadResponse
userInfo:@{kRLMSyncUnderlyingErrorKey: error}];
NSError *syncError;
if ([error.domain isEqualToString:RLMSyncErrorDomain]) {
// Network client may return sync related error
syncError = error;
} else {
// Something else went wrong
syncError = [NSError errorWithDomain:RLMSyncErrorDomain
code:RLMSyncErrorBadResponse
userInfo:@{kRLMSyncUnderlyingErrorKey: error}];
}

if (self.completionBlock) {
self.completionBlock(syncError);
}
Expand Down
8 changes: 8 additions & 0 deletions RealmSwift/Sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public typealias UserCompletionBlock = RLMUserCompletionBlock
*/
public typealias SyncError = RLMSyncError


/**
An error associated with the authentication to the Realm Object Server.

- see: `RLMSyncAuthError`
*/
public typealias SyncAuthError = RLMSyncAuthError

/**
An enum which can be used to specify the level of logging.

Expand Down

0 comments on commit 5a89982

Please sign in to comment.