Skip to content

Commit 4a396c2

Browse files
author
Sefa Ilkimen
committed
iOS: call failure handler when body can not be decoded
1 parent 53224ed commit 4a396c2

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/ios/TextResponseSerializer.m

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ - (NSString*)decodeResponseData:(NSData*)rawResponseData withEncoding:(CFStringE
6161
}
6262
}
6363

64-
if (!decoded) {
65-
decoded = @"Could not decode response data due to invalid or unknown charset encoding";
66-
}
67-
6864
return decoded;
6965
}
7066

@@ -82,22 +78,37 @@ - (CFStringEncoding) getEncoding:(NSURLResponse *)response {
8278

8379
- (BOOL)validateResponse:(NSHTTPURLResponse *)response
8480
data:(NSData *)data
81+
decoded:(NSString **)decoded
8582
error:(NSError * __autoreleasing *)error
8683
{
8784
BOOL responseIsValid = YES;
8885
NSError *validationError = nil;
8986

9087
if (response && [response isKindOfClass:[NSHTTPURLResponse class]]) {
91-
if (self.acceptableStatusCodes && ![self.acceptableStatusCodes containsIndex:(NSUInteger)response.statusCode] && [response URL]) {
88+
if (data) {
89+
*decoded = [self decodeResponseData:data withEncoding:[self getEncoding:response]];
90+
}
91+
92+
if (data && !*decoded) {
9293
NSMutableDictionary *mutableUserInfo = [@{
93-
NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: %@ (%ld)", @"AFNetworking", nil), [NSHTTPURLResponse localizedStringForStatusCode:response.statusCode], (long)response.statusCode],
9494
NSURLErrorFailingURLErrorKey:[response URL],
9595
AFNetworkingOperationFailingURLResponseErrorKey: response,
96+
AFNetworkingOperationFailingURLResponseDataErrorKey: data,
97+
AFNetworkingOperationFailingURLResponseBodyKey: @"Could not decode response data due to invalid or unknown charset encoding",
98+
} mutableCopy];
99+
100+
validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo], validationError);
101+
responseIsValid = NO;
102+
} else if (self.acceptableStatusCodes && ![self.acceptableStatusCodes containsIndex:(NSUInteger)response.statusCode] && [response URL]) {
103+
NSMutableDictionary *mutableUserInfo = [@{
104+
NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: %@ (%ld)", @"AFNetworking", nil), [NSHTTPURLResponse localizedStringForStatusCode:response.statusCode], (long)response.statusCode],
105+
NSURLErrorFailingURLErrorKey: [response URL],
106+
AFNetworkingOperationFailingURLResponseErrorKey: response,
96107
} mutableCopy];
97108

98109
if (data) {
99110
mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
100-
mutableUserInfo[AFNetworkingOperationFailingURLResponseBodyKey] = [self decodeResponseData:data withEncoding:[self getEncoding:response]];
111+
mutableUserInfo[AFNetworkingOperationFailingURLResponseBodyKey] = *decoded;
101112
}
102113

103114
validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo], validationError);
@@ -118,13 +129,15 @@ - (id)responseObjectForResponse:(NSURLResponse *)response
118129
data:(NSData *)data
119130
error:(NSError *__autoreleasing *)error
120131
{
121-
if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error]) {
132+
NSString* decoded = nil;
133+
134+
if (![self validateResponse:(NSHTTPURLResponse *)response data:data decoded:&decoded error:error]) {
122135
if (!error || AFErrorOrUnderlyingErrorHasCodeInDomain(*error, NSURLErrorCannotDecodeContentData, AFURLResponseSerializationErrorDomain)) {
123136
return nil;
124137
}
125138
}
126139

127-
return [self decodeResponseData:data withEncoding:[self getEncoding:response]];
140+
return decoded;
128141
}
129142

130143
@end

0 commit comments

Comments
 (0)