Skip to content

Commit

Permalink
Response headers to image - iOS (#33880)
Browse files Browse the repository at this point in the history
Summary:
Please see this issue #33034 for details on the problem solved by this PR.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[CATEGORY] [TYPE] - Message
[ios] [changed] - HTTP Response headers added to the error object passed to JS code.

Pull Request resolved: #33880

Test Plan:
Tested:
(All tests done on images in rn-tester app, which is a part of this repo)
1. onError method in case image has an HTTP Error
2. onError method in case of non http error (DNS error)
3. Successful image load

Reviewed By: cortinico

Differential Revision: D37066159

Pulled By: cipolleschi

fbshipit-source-id: 546f7678fa0321fe6c6fbef55288715cb6ddc2fd
  • Loading branch information
Supreet Singh authored and facebook-github-bot committed Jun 10, 2022
1 parent 6d2872d commit 9eb2826
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Libraries/Image/RCTImageLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ static uint64_t monotonicTimeGetCurrentNanoseconds(void)
return (mach_absolute_time() * tb_info.numer) / tb_info.denom;
}

static NSError* addResponseHeadersToError(NSError* originalError, NSHTTPURLResponse* response) {
NSMutableDictionary<NSString*, id>* _userInfo = (NSMutableDictionary<NSString*, id>*)originalError.userInfo.mutableCopy;
_userInfo[@"httpStatusCode"] = [NSNumber numberWithInt:response.statusCode];
_userInfo[@"httpResponseHeaders"] = response.allHeaderFields;
NSError *error = [NSError errorWithDomain:originalError.domain code:originalError.code userInfo:_userInfo];

return error;
}

@interface RCTImageLoader() <NativeImageLoaderIOSSpec, RCTImageLoaderWithAttributionProtocol>

@end
Expand Down Expand Up @@ -523,6 +532,10 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
}
});
} else if (!std::atomic_load(cancelled.get())) {
if (response && error && [response isKindOfClass: [NSHTTPURLResponse class]]) {
NSHTTPURLResponse* _httpResp = (NSHTTPURLResponse*)response;
error = addResponseHeadersToError(error, _httpResp);
}
completionBlock(error, imageOrData, imageMetadata, cacheResult, response);
}
};
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Image/RCTImageView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ - (void)imageLoaderLoadedImage:(UIImage *)loadedImage error:(NSError *)error for
});

if (_onError) {
_onError(@{ @"error": error.localizedDescription });
_onError(@{ @"error": error.localizedDescription, @"responseCode": (error.userInfo[@"httpStatusCode"]?: [NSNull null]), @"httpResponseHeaders": (error.userInfo[@"httpResponseHeaders"] ?: [NSNull null]) });
}
if (_onLoadEnd) {
_onLoadEnd(nil);
Expand Down

0 comments on commit 9eb2826

Please sign in to comment.