@@ -389,32 +389,56 @@ - (void)URLSession:(NSURLSession *)session task :(NSURLSessionTask *)task
389
389
390
390
if (_outer)
391
391
{
392
- if (error) {
392
+ if (error)
393
+ {
393
394
std::vector<unsigned char > buf; // just a placeholder
394
395
_outer->onTaskFinish (*[wrapper get ],
395
396
cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL,
396
397
(int )error.code ,
397
398
[error.localizedDescription cStringUsingEncoding: NSUTF8StringEncoding],
398
399
buf);
399
400
}
400
- else if (![wrapper get ]->storagePath .length ()) {
401
+ else if (![wrapper get ]->storagePath .length ())
402
+ {
401
403
// call onTaskFinish for a data task
402
404
// (for a file download task, callback is called in didFinishDownloadingToURL)
403
- std::string errorString = cocos2d::StringUtils::format ( " Downloader: Failed to download %s with no error " , [task.originalRequest.URL.absoluteString cStringUsingEncoding: NSUTF8StringEncoding]) ;
404
-
405
+ std::string errorString;
406
+
405
407
const int64_t buflen = [wrapper totalBytesReceived ];
406
408
char buf[buflen];
407
-
408
409
[wrapper transferDataToBuffer: buf lengthOfBuffer: buflen];
409
-
410
410
std::vector<unsigned char > data (buf, buf + buflen);
411
411
412
412
_outer->onTaskFinish (*[wrapper get ],
413
413
cocos2d::network::DownloadTask::ERROR_NO_ERROR,
414
- 0 ,
414
+ 0 ,
415
415
errorString,
416
416
data);
417
417
}
418
+ else
419
+ {
420
+ NSInteger statusCode = ((NSHTTPURLResponse *)task.response ).statusCode ;
421
+ std::vector<unsigned char > buf; // just a placeholder
422
+ const char *orignalURL = [task.originalRequest.URL.absoluteString cStringUsingEncoding: NSUTF8StringEncoding];
423
+ std::string errorMessage = " " ;
424
+
425
+ // Check for error status code
426
+ if (statusCode >= 400 )
427
+ {
428
+ errorMessage = cocos2d::StringUtils::format (" Downloader: Failed to download %s with status code (%d)" , orignalURL, (int )statusCode);
429
+ }
430
+ // Unknown issue
431
+ else
432
+ {
433
+ errorMessage = cocos2d::StringUtils::format (" Downloader: Failed to download %s with unknown error" , orignalURL);
434
+ }
435
+
436
+ _outer->onTaskFinish (*[wrapper get ],
437
+ cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL,
438
+ 0 ,
439
+ errorMessage,
440
+ buf);
441
+ }
418
442
}
419
443
[self .taskDict removeObjectForKey: task];
420
444
[wrapper release ];
@@ -511,22 +535,20 @@ - (void)URLSession:(NSURLSession *)session downloadTask :(NSURLSessionDownloadTa
511
535
return ;
512
536
}
513
537
514
- DownloadTaskWrapper *wrapper = [self .taskDict objectForKey: downloadTask];
515
- const char * storagePath = [wrapper get ]->storagePath .c_str ();
516
- NSString *destPath = [NSString stringWithUTF8String: storagePath];
517
- NSFileManager *fileManager = [NSFileManager defaultManager ];
518
- NSURL *destURL = nil ;
519
-
520
538
// On iOS 9 a response with status code 4xx(Client Error) or 5xx(Server Error)
521
539
// might end up calling this delegate method, saving the error message to the storage path
522
540
// and treating this download task as a successful one, so we need to check the status code here
523
541
NSInteger statusCode = ((NSHTTPURLResponse *)downloadTask.response ).statusCode ;
524
- if (statusCode >= 400 ) {
525
- std::vector<unsigned char > buf; // just a placeholder
526
- std::string response = cocos2d::StringUtils::format (" Downloader: Failed to download %s with status code (%d)" , [downloadTask.originalRequest.URL.absoluteString cStringUsingEncoding: NSUTF8StringEncoding], (int )statusCode);
527
- _outer->onTaskFinish (*[wrapper get ], cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL, noErr , response, buf);
542
+ if (statusCode >= 400 )
543
+ {
528
544
return ;
529
545
}
546
+
547
+ DownloadTaskWrapper *wrapper = [self .taskDict objectForKey: downloadTask];
548
+ const char * storagePath = [wrapper get ]->storagePath .c_str ();
549
+ NSString *destPath = [NSString stringWithUTF8String: storagePath];
550
+ NSFileManager *fileManager = [NSFileManager defaultManager ];
551
+ NSURL *destURL = nil ;
530
552
531
553
do
532
554
{
0 commit comments