Skip to content

Commit

Permalink
Remove NSUnderlyingErrorKey when it is an NSNull object (#135)
Browse files Browse the repository at this point in the history
* Remove `NSUnderlyingErrorKey` when it is an `NSNull` object

- NSErrors that are return from Intercom can have a value of `NSNull`. ReactNative is unable to handle this so we
strip them out to avoid crashing the app.

* Update IntercomModule.m
  • Loading branch information
Br1an-Boyle authored Oct 3, 2023
1 parent a473e74 commit b8d3c24
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
8 changes: 4 additions & 4 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ PODS:
- fmt (6.2.1)
- glog (0.3.5)
- Intercom (16.0.1)
- intercom-react-native (5.3.1):
- intercom-react-native (6.0.0):
- Intercom (~> 16.0.1)
- React-Core
- libevent (2.1.12)
Expand Down Expand Up @@ -519,7 +519,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
boost: a7c83b31436843459a1961bfd74b96033dc77234
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54
DoubleConversion: 831926d9b8bf8166fd87886c4abab286c2422662
FBLazyVector: bc76253beb7463b688aa6af913b822ed631de31a
FBReactNativeSpec: 85d34420d92cb178897de05e3aba90e7a8568162
Flipper: 26fc4b7382499f1281eb8cb921e5c3ad6de91fe0
Expand All @@ -532,9 +532,9 @@ SPEC CHECKSUMS:
Flipper-RSocket: d9d9ade67cbecf6ac10730304bf5607266dd2541
FlipperKit: cbdee19bdd4e7f05472a66ce290f1b729ba3cb86
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
glog: 5337263514dd6f09803962437687240c5dc39aa4
Intercom: 62fe4d94519fba99f17df3f7a0c62dc7dbcb7b02
intercom-react-native: bfa2ea64fba2b38f3b0c48afe020b05ac0007cb3
intercom-react-native: 7bf5734cca0629303b514e188e60d7d87c17338c
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
RCT-Folly: 0080d0a6ebf2577475bda044aa59e2ca1f909cda
Expand Down
22 changes: 19 additions & 3 deletions ios/IntercomModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ - (NSData *)dataFromHexString:(NSString *)string {
[Intercom loginUnidentifiedUserWithSuccess:^{
successCallback(@(YES));
} failure:^(NSError * _Nonnull error) {
failureCallback(error);
failureCallback([self removeNullUnderlyingError:error]);
}];
};

Expand All @@ -119,7 +119,7 @@ - (NSData *)dataFromHexString:(NSString *)string {
[Intercom loginUserWithUserAttributes:attributes success:^{
successCallback(@(YES));
} failure:^(NSError * _Nonnull error) {
failureCallback(error);
failureCallback([self removeNullUnderlyingError:error]);
}];
}

Expand All @@ -135,7 +135,7 @@ - (NSData *)dataFromHexString:(NSString *)string {
[Intercom updateUser:userAttributes success:^{
resolve(@(YES));
} failure:^(NSError * _Nonnull error) {
failureCallback(error);
failureCallback([self removeNullUnderlyingError:error]);
}];
};

Expand Down Expand Up @@ -360,4 +360,20 @@ - (NSError *)exceptionToError:(NSException *)exception :(NSString *)code :(NSStr

return [[NSError alloc] initWithDomain:domain code:[code integerValue] userInfo:info];
};


/// Remove `NSUnderlyingErrorKey` from the `userInfo` if its value is of type `NSNull`
///
/// NSErrors that are return from Intercom can have a value of `NSNull`. ReactNative is unable to handle this so we
/// strip them out to avoid crashing the app.
/// - Parameter error: the `NSError` object.
- (NSError *)removeNullUnderlyingError:(NSError *)error {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
NSError *underlyingError = [error.userInfo objectForKey:NSUnderlyingErrorKey];
[userInfo addEntriesFromDictionary:error.userInfo];
if([underlyingError isKindOfClass:[NSNull class]]) {
[userInfo removeObjectForKey:NSUnderlyingErrorKey];
}
return [[NSError alloc] initWithDomain:error.domain code:error.code userInfo:userInfo];
};
@end

0 comments on commit b8d3c24

Please sign in to comment.