AFHTTPSessionManager does not pass JSON error responses to failure block

AFHTTPSessionManager does not pass JSON error responses to failure block
以往採用AFNetworking 2.x呼叫API後,無論在success或failure的block,都可以取得http response資訊,從而依需求取得所需的資訊,像是API執行失敗後回拋的說明資料等等
但大升級到AFNetworking 3.x後,發現failure block不再提供response相關資訊,其實是全部統一包進NSError了,二種版本的差異如下

AFNetworking 2.x
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation ,id JSON) {
  // do something if success
 } failure:^(AFHTTPRequestOperation *operation , NSError *error) {
  // do something if failure
        NSData *data = [operation.responseString dataUsingEncoding:NSUTF8StringEncoding];
        id dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [self showJWTError:[dict objectForKey:@"error_description"]];
}];

AFNetworking 3.x
[manager POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
// do something if success
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  // do something if failure
        NSData *data = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey];
        NSDictionary *serializedData = [NSJSONSerialization JSONObjectWithData: data options:kNilOptions error:nil];
        [self showJWTError:[serializedData objectForKey:@"error_description"]];
}];

ps: showJWTError是我用來呈現錯誤資訊給用戶的自訂方法

沒有留言: