@@ -185,45 +185,24 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
185185 id responseObj = nil ;
186186 NSError *error = nil ;
187187 switch (_task.responseType ) {
188- case STHTTPNetTaskResponseRawData: {
188+ case STHTTPNetTaskResponseRawData:
189189 responseObj = data;
190- }
191190 break ;
192- case STHTTPNetTaskResponseString: {
193- @try {
194- if (data.length ) {
195- responseObj = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
196- }
197- else {
198- responseObj = @" " ;
199- }
200- }
201- @catch (NSException *exception) {
202- [STNetTaskQueueLog log: @" Response parsed error: %@ " , exception.debugDescription];
203- error = [NSError errorWithDomain: STHTTPNetTaskResponseParsedError
204- code: 0
205- userInfo: @{ @" url" : httpResponse.URL .absoluteString }];
206- }
207- }
191+ case STHTTPNetTaskResponseString:
192+ responseObj = [self stringFromData: data];
208193 break ;
209194 case STHTTPNetTaskResponseJSON:
210- default : {
211- if (data.length ) {
212- responseObj = [NSJSONSerialization JSONObjectWithData: data options: kNilOptions error: &error];
213- if (error) {
214- [STNetTaskQueueLog log: @" Response parsed error: %@ " , error.debugDescription];
215- error = [NSError errorWithDomain: STHTTPNetTaskResponseParsedError
216- code: 0
217- userInfo: @{ @" url" : httpResponse.URL .absoluteString }];
218- }
219- }
220- else {
221- responseObj = @{};
222- }
223- }
195+ default :
196+ responseObj = [self JSONFromData: data];
224197 break ;
225198 }
226199
200+ if (!responseObj) {
201+ error = [NSError errorWithDomain: STHTTPNetTaskResponseParsedError
202+ code: 0
203+ userInfo: @{ @" url" : httpResponse.URL .absoluteString }];
204+ }
205+
227206 if (error) {
228207 [_queue task: _task didFailWithError: error];
229208 }
@@ -232,7 +211,7 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
232211 }
233212 }
234213 else {
235- if (!error) { // Response status code is not 200
214+ if (!error) { // Response status code is not 20x
236215 error = [NSError errorWithDomain: STHTTPNetTaskServerError
237216 code: 0
238217 userInfo: @{ STHTTPNetTaskErrorStatusCodeUserInfoKey: @(httpResponse.statusCode ),
@@ -243,7 +222,32 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
243222 }
244223}
245224
246- #pragma mark - Data construct methods
225+ #pragma makr - Response data parsing methods
226+
227+ - (NSString *)stringFromData : (NSData *)data
228+ {
229+ @try {
230+ NSString *string = data.length ? [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding] : @" " ;
231+ return string;
232+ }
233+ @catch (NSException *exception) {
234+ [STNetTaskQueueLog log: @" String parsed error: %@ " , exception.debugDescription];
235+ return nil ;
236+ }
237+ }
238+
239+ - (id )JSONFromData : (NSData *)data
240+ {
241+ NSError *error;
242+ id JSON = data.length ? [NSJSONSerialization JSONObjectWithData: data options: kNilOptions error: &error] : @{};
243+ if (error) {
244+ [STNetTaskQueueLog log: @" JSON parsed error: %@ " , error.debugDescription];
245+ return nil ;
246+ }
247+ return JSON;
248+ }
249+
250+ #pragma mark - Request data constructing methods
247251
248252- (NSString *)queryStringFromParameters : (NSDictionary *)parameters
249253{
0 commit comments