Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove non-serializable objects from error.userData when request fails #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions InAppUtils/InAppUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,18 @@ - (void)productsRequest:(SKProductsRequest *)request
}

// SKProductsRequestDelegate network error
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
NSString *key = RCTKeyForInstance(request);
RCTResponseSenderBlock callback = _callbacks[key];
if(callback) {
if (callback) {
// Ensure error.userData can be converted to JSON without error.
// This will remove any NSURL from the error.userData.
error = RCTErrorClean(error);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be upstream in react-native? they should handle NSURL in RCTJSErrorFromNSError?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, but upstreaming it and waiting for merge will take pretty long and till then this method will crash.

I will try to get fb to merge a change to support NSURL too

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. let's get a PR upstream. once it's merged upstream, we can add a fallback here. If it's not merged upstream in x time, then let's ship here.

that works for you?

callback(@[RCTJSErrorFromNSError(error)]);
[_callbacks removeObjectForKey:key];
} else {
RCTLogWarn(@"No callback registered for request error.");
}
}

Expand All @@ -241,4 +247,16 @@ - (void)dealloc
return [NSString stringWithFormat:@"%p", instance];
}

static NSError *RCTErrorClean(NSError *error)
{
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
[RCTJSONClean(error.userInfo) enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
if ([key isKindOfClass:NSString.class] && ![value isKindOfClass:NSNull.class]) {
userInfo[key] = value;
}
}];

return [NSError errorWithDomain:error.domain code:error.code userInfo:userInfo];
}

@end