由AFN网络请求失败不曾返回错误信息引发的思考

2020-04-11  本文已影响0人  墨_辰

由AFN网络请求失败不曾返回错误信息引发的思考:

业务场景:
(NSURLSessionDataTask*)dataTaskWithHTTPMethod:(NSString*)method
                                          URLString:(NSString*)URLString
                                         parameters:(id)parameters
                                     uploadProgress:(nullablevoid(^)(NSProgress*uploadProgress)) uploadProgress
                                   downloadProgress:(nullablevoid(^)(NSProgress*downloadProgress)) downloadProgress
                                            success:(void(^)(NSURLSessionDataTask*, id))success
                                            failure:(void(^)(NSURLSessionDataTask*, NSError*))failure
    {
       NSError*serializationError=nil;
       NSMutableURLRequest*request=[self.requestSerializerrequestWithMethod:methodURLString:[[NSURLURLWithString:URLStringrelativeToURL:self.baseURL] absoluteString] parameters:parameterserror:&serializationError];
       if(serializationError) {
           if(failure) {
               dispatch_async(self.completionQueue?: dispatch_get_main_queue(), ^{
                   failure(nil, serializationError);
              });
          }
    ​
           returnnil;
      }
    ​
       __blockNSURLSessionDataTask*dataTask=nil;
       dataTask=[selfdataTaskWithRequest:request
                             uploadProgress:uploadProgress
                           downloadProgress:downloadProgress
                          completionHandler:^(NSURLResponse*__unusedresponse, idresponseObject, NSError*error) {
           if(error) {
               if(failure) {
                   failure(dataTask, error);
              }
          } else{
               if(success) {
                   success(dataTask, responseObject);
              }
          }
      }];
    ​
       returndataTask;
    }
NSData*errorData=[error.userInfoobjectForKey:AFNetworkingOperationFailingURLResponseDataErrorKey];
NSDictionary*dic=[NSJSONSerializationJSONObjectWithData:errorDataoptions:NSJSONReadingAllowFragmentserror:nil];     

Error 的返回包括了下述三个字段,都可以在error.userInfo中取出,其中的AFNetworkingOperationFailingURLResponseDataErrorKey就是我们需要的错误信息。将其转化为DIctionary就可以获取我需要的值了。

NSString*constAFURLResponseSerializationErrorDomain=@"com.alamofire.error.serialization.response";
NSString*constAFNetworkingOperationFailingURLResponseErrorKey=@"com.alamofire.serialization.response.error.response";
NSString*constAFNetworkingOperationFailingURLResponseDataErrorKey=@"com.alamofire.serialization.response.error.data";
思考
为什么失败信息会在成功回调和失败回调中都返回,这样的话AFN区分成功回调和失败回调有什么意义呢?

问了公司大佬得到的解释:
我说到的两种错误信息是不同的,错误信息包括网络错误信息和业务错误信息。

为了适应公司的需求,我们认为把AFN提供的successfailure的回调合成一个completionHandler,返回信息包括responseObject会更合适一点。最后封装成如下的方式:
+(NSURLSessionTask*) doPost:(AFHTTPSessionManager*) manageUrl:(NSString*) urlParams:(NSDictionary*) paramsCompletionHandler:(void(^)(NSURLSessionDataTask*task, idresponseObject)) completionHandler{
      return[managePOST:urlparameters:paramsprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask, id _NonnullresponseObject) {
               completionHandler(task, responseObject);
          } failure:^(NSURLSessionDataTask*_Nullabletask, NSError*_Nonnullerror) {

               NSData*errorData=[error.userInfoobjectForKey:AFNetworkingOperationFailingURLResponseDataErrorKey];
               NSDictionary*dic=[NSJSONSerializationJSONObjectWithData:errorDataoptions:NSJSONReadingAllowFragmentserror:nil];
               completionHandler(task, dic);

          }];
        }
上一篇下一篇

猜你喜欢

热点阅读