@@ -61,10 +61,6 @@ - (NSString*)decodeResponseData:(NSData*)rawResponseData withEncoding:(CFStringE
61
61
}
62
62
}
63
63
64
- if (!decoded) {
65
- decoded = @" Could not decode response data due to invalid or unknown charset encoding" ;
66
- }
67
-
68
64
return decoded;
69
65
}
70
66
@@ -82,22 +78,37 @@ - (CFStringEncoding) getEncoding:(NSURLResponse *)response {
82
78
83
79
- (BOOL )validateResponse : (NSHTTPURLResponse *)response
84
80
data : (NSData *)data
81
+ decoded : (NSString **)decoded
85
82
error : (NSError * __autoreleasing *)error
86
83
{
87
84
BOOL responseIsValid = YES ;
88
85
NSError *validationError = nil ;
89
86
90
87
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) {
92
93
NSMutableDictionary *mutableUserInfo = [@{
93
- NSLocalizedDescriptionKey : [NSString stringWithFormat: NSLocalizedStringFromTable(@" Request failed: %@ (%ld )" , @" AFNetworking" , nil ), [NSHTTPURLResponse localizedStringForStatusCode: response.statusCode], (long )response.statusCode],
94
94
NSURLErrorFailingURLErrorKey :[response URL ],
95
95
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,
96
107
} mutableCopy];
97
108
98
109
if (data) {
99
110
mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
100
- mutableUserInfo[AFNetworkingOperationFailingURLResponseBodyKey] = [ self decodeResponseData: data withEncoding: [ self getEncoding: response]] ;
111
+ mutableUserInfo[AFNetworkingOperationFailingURLResponseBodyKey] = *decoded ;
101
112
}
102
113
103
114
validationError = AFErrorWithUnderlyingError ([NSError errorWithDomain: AFURLResponseSerializationErrorDomain code: NSURLErrorBadServerResponse userInfo: mutableUserInfo], validationError);
@@ -118,13 +129,15 @@ - (id)responseObjectForResponse:(NSURLResponse *)response
118
129
data : (NSData *)data
119
130
error : (NSError *__autoreleasing *)error
120
131
{
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]) {
122
135
if (!error || AFErrorOrUnderlyingErrorHasCodeInDomain (*error, NSURLErrorCannotDecodeContentData, AFURLResponseSerializationErrorDomain)) {
123
136
return nil ;
124
137
}
125
138
}
126
139
127
- return [ self decodeResponseData: data withEncoding: [ self getEncoding: response]] ;
140
+ return decoded ;
128
141
}
129
142
130
143
@end
0 commit comments