From f3d68c9cc26c8016a8b528a4f0e3c91f362c9c7d Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 7 May 2021 16:40:10 +0800 Subject: [PATCH] Added `SDWebImageErrorDownloadResponseKey` userInfo for better error report --- .../Core/SDWebImageDownloaderOperation.m | 21 ++++++++++++++----- SDWebImage/Core/SDWebImageError.h | 2 ++ SDWebImage/Core/SDWebImageError.m | 2 ++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/SDWebImage/Core/SDWebImageDownloaderOperation.m b/SDWebImage/Core/SDWebImageDownloaderOperation.m index 34a98adc..a2395c5c 100644 --- a/SDWebImage/Core/SDWebImageDownloaderOperation.m +++ b/SDWebImage/Core/SDWebImageDownloaderOperation.m @@ -191,6 +191,7 @@ typedef NSMutableDictionary SDCallbacksDictionary; } if (cachedResponse) { self.cachedData = cachedResponse.data; + self.response = cachedResponse.response; } } @@ -323,7 +324,9 @@ didReceiveResponse:(NSURLResponse *)response response = [self.responseModifier modifiedResponseWithResponse:response]; if (!response) { valid = NO; - self.responseError = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorInvalidDownloadResponse userInfo:@{NSLocalizedDescriptionKey : @"Download marked as failed because response is nil"}]; + self.responseError = [NSError errorWithDomain:SDWebImageErrorDomain + code:SDWebImageErrorInvalidDownloadResponse + userInfo:@{NSLocalizedDescriptionKey : @"Download marked as failed because response is nil"}]; } } @@ -342,7 +345,9 @@ didReceiveResponse:(NSURLResponse *)response valid = NO; self.responseError = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorInvalidDownloadStatusCode - userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Download marked as failed because of invalid response status code %ld", (long)statusCode], SDWebImageErrorDownloadStatusCodeKey: @(statusCode)}]; + userInfo:@{NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Download marked as failed because of invalid response status code %ld", (long)statusCode], + SDWebImageErrorDownloadStatusCodeKey : @(statusCode), + SDWebImageErrorDownloadResponseKey : response}]; } // Check content type valid (defaults nil) NSString *contentType = [response isKindOfClass:NSHTTPURLResponse.class] ? ((NSHTTPURLResponse *)response).MIMEType : nil; @@ -354,7 +359,9 @@ didReceiveResponse:(NSURLResponse *)response valid = NO; self.responseError = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorInvalidDownloadContentType - userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Download marked as failed because of invalid response content type %@", contentType], SDWebImageErrorDownloadContentTypeKey: contentType}]; + userInfo:@{NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Download marked as failed because of invalid response content type %@", contentType], + SDWebImageErrorDownloadContentTypeKey : contentType, + SDWebImageErrorDownloadResponseKey : response}]; } //'304 Not Modified' is an exceptional one //URLSession current behavior will return 200 status code when the server respond 304 and URLCache hit. But this is not a standard behavior and we just add a check @@ -362,7 +369,8 @@ didReceiveResponse:(NSURLResponse *)response valid = NO; self.responseError = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorCacheNotModified - userInfo:@{NSLocalizedDescriptionKey: @"Download response status code is 304 not modified and ignored"}]; + userInfo:@{NSLocalizedDescriptionKey: @"Download response status code is 304 not modified and ignored", + SDWebImageErrorDownloadResponseKey : response}]; } if (valid) { @@ -495,7 +503,10 @@ didReceiveResponse:(NSURLResponse *)response * then we should check if the cached data is equal to image data */ if (self.options & SDWebImageDownloaderIgnoreCachedResponse && [self.cachedData isEqualToData:imageData]) { - self.responseError = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorCacheNotModified userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image is not modified and ignored"}]; + self.responseError = [NSError errorWithDomain:SDWebImageErrorDomain + code:SDWebImageErrorCacheNotModified + userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image is not modified and ignored", + SDWebImageErrorDownloadResponseKey : self.response}]; // call completion block with not modified error [self callCompletionBlocksWithError:self.responseError]; [self done]; diff --git a/SDWebImage/Core/SDWebImageError.h b/SDWebImage/Core/SDWebImageError.h index acae0801..bb91d0bd 100644 --- a/SDWebImage/Core/SDWebImageError.h +++ b/SDWebImage/Core/SDWebImageError.h @@ -11,6 +11,8 @@ FOUNDATION_EXPORT NSErrorDomain const _Nonnull SDWebImageErrorDomain; +/// The response instance for invalid download response (NSURLResponse *) +FOUNDATION_EXPORT NSErrorUserInfoKey const _Nonnull SDWebImageErrorDownloadResponseKey; /// The HTTP status code for invalid download response (NSNumber *) FOUNDATION_EXPORT NSErrorUserInfoKey const _Nonnull SDWebImageErrorDownloadStatusCodeKey; /// The HTTP MIME content type for invalid download response (NSString *) diff --git a/SDWebImage/Core/SDWebImageError.m b/SDWebImage/Core/SDWebImageError.m index 29a7039b..bd0d17ad 100644 --- a/SDWebImage/Core/SDWebImageError.m +++ b/SDWebImage/Core/SDWebImageError.m @@ -10,5 +10,7 @@ #import "SDWebImageError.h" NSErrorDomain const _Nonnull SDWebImageErrorDomain = @"SDWebImageErrorDomain"; + +NSErrorUserInfoKey const _Nonnull SDWebImageErrorDownloadResponseKey = @"SDWebImageErrorDownloadResponseKey"; NSErrorUserInfoKey const _Nonnull SDWebImageErrorDownloadStatusCodeKey = @"SDWebImageErrorDownloadStatusCodeKey"; NSErrorUserInfoKey const _Nonnull SDWebImageErrorDownloadContentTypeKey = @"SDWebImageErrorDownloadContentTypeKey";