Skip to content

Commit 9e6feb0

Browse files
authored
Merge pull request #952 from OneSignal/fix/failure_block_in_send_tags
Calling the failure block of send tags for bad input
2 parents a8a4113 + d2bd2e4 commit 9e6feb0

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,17 +1248,27 @@ + (void)sendTags:(NSDictionary*)keyValuePair {
12481248
+ (void)sendTags:(NSDictionary*)keyValuePair onSuccess:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock {
12491249

12501250
// return if the user has not granted privacy permissions
1251-
if ([self shouldLogMissingPrivacyConsentErrorWithMethodName:@"sendTags:onSuccess:onFailure:"])
1251+
if ([self shouldLogMissingPrivacyConsentErrorWithMethodName:@"sendTags:onSuccess:onFailure:"]) {
1252+
NSError *error = [NSError errorWithDomain:@"com.onesignal.tags" code:0 userInfo:@{@"error" : @"Your application has called sendTags:onSuccess:onFailure: before the user granted privacy permission. Please call `consentGranted(bool)` in order to provide user privacy consent"}];
1253+
failureBlock(error);
12521254
return;
1255+
}
1256+
12531257

12541258
if (![NSJSONSerialization isValidJSONObject:keyValuePair]) {
1255-
onesignal_Log(ONE_S_LL_WARN, [NSString stringWithFormat:@"sendTags JSON Invalid: The following key/value pairs you attempted to send as tags are not valid JSON: %@", keyValuePair]);
1259+
NSString *errorMessage = [NSString stringWithFormat:@"sendTags JSON Invalid: The following key/value pairs you attempted to send as tags are not valid JSON: %@", keyValuePair];
1260+
onesignal_Log(ONE_S_LL_WARN, errorMessage);
1261+
NSError *error = [NSError errorWithDomain:@"com.onesignal.tags" code:0 userInfo:@{@"error" : errorMessage}];
1262+
failureBlock(error);
12561263
return;
12571264
}
12581265

12591266
for (NSString *key in [keyValuePair allKeys]) {
12601267
if ([keyValuePair[key] isKindOfClass:[NSDictionary class]]) {
1261-
onesignal_Log(ONE_S_LL_WARN, @"sendTags Tags JSON must not contain nested objects");
1268+
NSString *errorMessage = @"sendTags Tags JSON must not contain nested objects";
1269+
onesignal_Log(ONE_S_LL_WARN, errorMessage);
1270+
NSError *error = [NSError errorWithDomain:@"com.onesignal.tags" code:0 userInfo:@{@"error" : errorMessage}];
1271+
failureBlock(error);
12621272
return;
12631273
}
12641274
}

iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,15 +1388,23 @@ - (void)testSendBadObjectInTags {
13881388
[UnitTestCommonMethods initOneSignal_andThreadWait];
13891389

13901390
XCTAssertEqual(OneSignalClientOverrider.networkRequestCount, 2);
1391+
XCTestExpectation *errorExpectation = [self expectationWithDescription:@"onesignal_failure_block_called"];
1392+
errorExpectation.expectedFulfillmentCount = 1;
13911393
//The OneSignal class is not a valid json object
1392-
[OneSignal sendTags:@{@"key1": @"value1", @"key2": [OneSignal new]}];
1394+
[OneSignal sendTags:@{@"key1": @"value1", @"key2": [OneSignal new]} onSuccess:^(NSDictionary *result) {
1395+
XCTAssertNotNil(nil); //Assert if success is called
1396+
} onFailure:^(NSError *error) {
1397+
XCTAssertEqualObjects(error.domain, @"com.onesignal.tags");
1398+
[errorExpectation fulfill];
1399+
}];
13931400

13941401
// Make sure the tags were not sent.
13951402
[NSObjectOverrider runPendingSelectors];
13961403
[UnitTestCommonMethods runBackgroundThreads];
13971404
[NSObjectOverrider runPendingSelectors];
13981405

13991406
XCTAssertEqual(OneSignalClientOverrider.networkRequestCount, 2);
1407+
[self waitForExpectations:@[errorExpectation] timeout:1];
14001408
}
14011409

14021410
- (void)testDeleteTags {

0 commit comments

Comments
 (0)