Skip to content

Commit 2cb2fad

Browse files
committed
SDK-76 remove NSException on branchKey errors. replace with NSError. Also add unit tests
1 parent 57fba16 commit 2cb2fad

File tree

3 files changed

+107
-15
lines changed

3 files changed

+107
-15
lines changed

Branch-SDK/Branch-SDK/Branch.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,24 @@ typedef NS_ENUM(NSUInteger, BranchCreditHistoryOrder) {
225225
You can only set the Branch key once per app run.
226226
227227
@param branchKey The Branch key to use.
228+
@param error NSError will be set if Branch encounters a key error.
228229
*/
230+
+ (void) setBranchKey:(NSString*)branchKey error:(NSError **)error;
231+
232+
/**
233+
Directly sets the Branch key to be used. Branch usually reads the Branch key from your app's
234+
Info.plist file which is recommended and more convenient. But the Branch key can also be set
235+
with this method. See the documentation at
236+
https://dev.branch.io/getting-started/sdk-integration-guide/guide/ios/#configure-xcode-project
237+
for information about configuring your app with Branch keys.
238+
239+
You can only set the Branch key once per app run. Any errors are logged.
240+
241+
@param branchKey The Branch key to use.
242+
*/
229243
+ (void) setBranchKey:(NSString*)branchKey;
230244

245+
231246
/// @return Returns the current Branch key.
232247
+ (NSString*) branchKey;
233248

Branch-SDK/Branch-SDK/Branch.m

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ + (BranchActivityItemProvider *)getBranchActivityItemWithParams:(NSDictionary *)
339339
static NSString *bnc_branchKey = nil;
340340
static BOOL bnc_enableFingerprintIDInCrashlyticsReports = YES;
341341

342+
+ (void) resetBranchKey {
343+
bnc_branchKey = nil;
344+
}
345+
342346
+ (void) setUseTestBranchKey:(BOOL)useTestKey {
343347
@synchronized (self) {
344348
if (bnc_branchKey && !!useTestKey != !!bnc_useTestBranchKey) {
@@ -355,36 +359,51 @@ + (BOOL) useTestBranchKey {
355359
}
356360
}
357361

358-
+ (void) setBranchKey:(NSString*)branchKey {
362+
+ (void)setBranchKey:(NSString *)branchKey {
363+
NSError *error;
364+
[self setBranchKey:branchKey error:&error];
365+
366+
if (error) {
367+
BNCLogError(@"Branch init error: %@", error.localizedDescription);
368+
}
369+
}
370+
371+
+ (void)setBranchKey:(NSString*)branchKey error:(NSError **)error {
359372
@synchronized (self) {
360373
if (bnc_branchKey) {
361374
if (branchKey &&
362375
[branchKey isKindOfClass:[NSString class]] &&
363376
[branchKey isEqualToString:bnc_branchKey]) {
364377
return;
365378
}
366-
BNCLogError(@"Branch key can only be set once.");
379+
380+
NSString *errorMessage = [NSString stringWithFormat:@"Branch key can only be set once."];
381+
*error = [NSError branchErrorWithCode:BNCInitError localizedMessage:errorMessage];
367382
return;
368383
}
384+
369385
if (![branchKey isKindOfClass:[NSString class]]) {
370386
NSString *typeName = (branchKey) ? NSStringFromClass(branchKey.class) : @"<nil>";
371-
[NSException raise:NSInternalInconsistencyException
372-
format:@"Invalid Branch key of type '%@'.", typeName];
387+
388+
NSString *errorMessage = [NSString stringWithFormat:@"Invalid Branch key of type '%@'.", typeName];
389+
*error = [NSError branchErrorWithCode:BNCInitError localizedMessage:errorMessage];
373390
return;
374391
}
375392

376393
if ([branchKey hasPrefix:@"key_test"]) {
377394
bnc_useTestBranchKey = YES;
395+
// Consider making this an NSError? It's not really an error but users need to be aware they're using a test key.
378396
BNCLogWarning(
379397
@"You are using your test app's Branch Key. "
380398
"Remember to change it to live Branch Key for production deployment."
381399
);
382-
} else
383-
if ([branchKey hasPrefix:@"key_live"]) {
400+
401+
} else if ([branchKey hasPrefix:@"key_live"]) {
384402
bnc_useTestBranchKey = NO;
403+
385404
} else {
386-
[NSException raise:NSInternalInconsistencyException
387-
format:@"Invalid Branch key format. Did you add your Branch key to your Info.plist? Passed key is '%@'.", branchKey];
405+
NSString *errorMessage = [NSString stringWithFormat:@"Invalid Branch key format. Did you add your Branch key to your Info.plist? Passed key is '%@'.", branchKey];
406+
*error = [NSError branchErrorWithCode:BNCInitError localizedMessage:errorMessage];
388407
return;
389408
}
390409

Branch-TestBed/Branch-SDK-Unhosted-Tests/Branch_setBranchKeyTests.m

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,91 @@
99
#import <XCTest/XCTest.h>
1010
#import "Branch.h"
1111

12-
/**
13-
Tests pre-init methods. These must run unhosted in order to avoid initialization that the test app does.
14-
*/
12+
// expose private methods used by tests
13+
@interface Branch()
14+
+ (void) resetBranchKey;
15+
@end
16+
17+
// Tests pre-init methods. These run unhosted in order to avoid initialization that the test app does.
1518
@interface Branch_setBranchKeyTests : XCTestCase
1619

1720
@end
1821

1922
@implementation Branch_setBranchKeyTests
2023

2124
- (void)setUp {
22-
25+
[Branch resetBranchKey];
2326
}
2427

2528
- (void)tearDown {
2629

2730
}
2831

2932
- (void)testSetBranchKey_validKey {
30-
XCTAssertNoThrow([Branch setBranchKey:@"key_live_foo"]);
33+
NSString *testKey = @"key_live_foo";
34+
35+
// previous implementation would throw exceptions on misconfiguration, this is deprecated behavior.
36+
XCTAssertNoThrow([Branch setBranchKey:testKey]);
37+
XCTAssert([[Branch branchKey] isEqualToString:testKey]);
3138
}
3239

3340
- (void)testSetBranchKey_nilKey {
34-
XCTAssertNoThrow([Branch setBranchKey:nil]);
41+
NSString *testKey = nil;
42+
43+
// previous implementation would throw exceptions on misconfiguration, this is deprecated behavior.
44+
XCTAssertNoThrow([Branch setBranchKey:testKey]);
45+
XCTAssert([Branch branchKey] == nil);
3546
}
3647

3748
- (void)testSetBranchKey_invalidKey {
38-
XCTAssertNoThrow([Branch setBranchKey:@"invalid_key"]);
49+
NSString *testKey = @"invalid_key";
50+
51+
// previous implementation would throw exceptions on misconfiguration, this is deprecated behavior.
52+
XCTAssertNoThrow([Branch setBranchKey:testKey]);
53+
XCTAssert([Branch branchKey] == nil);
54+
}
55+
56+
- (void)testSetBranchKeyWithError_validKey {
57+
NSString *testKey = @"key_live_foo";
58+
59+
NSError *error = nil;
60+
[Branch setBranchKey:testKey error:&error];
61+
XCTAssertNil(error);
62+
63+
XCTAssert([[Branch branchKey] isEqualToString:testKey]);
64+
}
65+
66+
- (void)testSetBranchKeyWithError_nilKey {
67+
NSError *error = nil;
68+
[Branch setBranchKey:nil error:&error];
69+
XCTAssertNotNil(error);
70+
71+
XCTAssert([error.localizedFailureReason isEqualToString:@"Invalid Branch key of type '<nil>'."]);
72+
XCTAssert([Branch branchKey] == nil);
73+
}
74+
75+
- (void)testSetBranchKeyWithError_invalidKey {
76+
NSError *error = nil;
77+
[Branch setBranchKey:@"invalid_key" error:&error];
78+
XCTAssertNotNil(error);
79+
80+
XCTAssert([error.localizedFailureReason isEqualToString:@"Invalid Branch key format. Did you add your Branch key to your Info.plist? Passed key is 'invalid_key'."]);
81+
XCTAssert([Branch branchKey] == nil);
82+
}
83+
84+
- (void)testSetBranchKeyWithError_validKeyTwice {
85+
NSString *testKey = @"key_live_foo";
86+
87+
NSError *error = nil;
88+
[Branch setBranchKey:testKey error:&error];
89+
XCTAssertNil(error);
90+
XCTAssert([[Branch branchKey] isEqualToString:testKey]);
91+
92+
// Cannot change the key after it is set once
93+
[Branch setBranchKey:@"key_live_bar" error:&error];
94+
XCTAssertNotNil(error);
95+
XCTAssert([error.localizedFailureReason isEqualToString:@"Branch key can only be set once."]);
96+
XCTAssert([[Branch branchKey] isEqualToString:testKey]);
3997
}
4098

4199
@end

0 commit comments

Comments
 (0)