如何从Alamofire错误中获取基础错误?

时间:2017-03-04 04:04:32

标签: ios swift alamofire nserror

对于此请求:

Alamofire.request("https://google.com").responseCollection { (response: DataResponse<[User]>) in
  guard response.result.isSuccess else {
    print(response.error)

    return
  }
}

我在控制台中看到了这个:

  

可选(my_app_name.BackendError.jsonSerialization(Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误   Domain = NSCocoaErrorDomain Code = 3840&#34;字符周围的值无效   0&#34; UserInfo = {NSDebugDescription =字符0周围的值无效。}))))

我尝试过的事情:

Alamofire.request("https://google.com").responseCollection { (response: DataResponse<[User]>) in
  guard response.result.isSuccess else {
    print(response.error)

    if let error1 = response.error as? AFError {
      print(error1)  // Execution DOES NOT reach here.
    }

    if let error2 = response.error as? BackendError {
      print(error2) // Execution DOES reach here.
    }

    return
  }
}
上面的

print(error2)打印:

  

jsonSerialization(Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误   Domain = NSCocoaErrorDomain Code = 3840&#34;字符周围的值无效   0&#34; UserInfo = {NSDebugDescription =字符0周围的值无效。})))

我尝试做的是解决潜在的错误,以便我可以解析domaincodeuserInfo属性。

我在https://github.com/Alamofire/Alamofire#handling-errors创建了Alamofire提供的BackendError枚举:

enum BackendError: Error {
    case network(error: Error) // Capture any underlying Error from the URLSession API
    case dataSerialization(error: Error)
    case jsonSerialization(error: Error)
    case xmlSerialization(error: Error)
    case objectSerialization(reason: String)
}

我还实现了示例通用响应对象序列化,与https://github.com/Alamofire/Alamofire#generic-response-object-serialization处的示例完全相同:

extension DataRequest {
  @discardableResult
  func responseCollection<T: ResponseCollectionSerializable>(
    queue: DispatchQueue? = nil,
    completionHandler: @escaping (DataResponse<[T]>) -> Void) -> Self {
    let responseSerializer = DataResponseSerializer<[T]> { request, response, data, error in
      guard error == nil else {
        return .failure(BackendError.network(error: error!))
      }

      let jsonSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)
      let result = jsonSerializer.serializeResponse(request, response, data, nil)

      guard case let .success(jsonObject) = result else {
        return .failure(BackendError.jsonSerialization(error: result.error!))
      }

      guard let response = response else {
        let reason = "Response collection could not be serialized due to nil response."
        return .failure(BackendError.objectSerialization(reason: reason))
      }

      return .success(T.collection(from: response, withRepresentation: jsonObject))
    }

    return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
  }
}

我认为有switch es,case s和来自BackendErrorAFErrorError和/或{{1但是我似乎无法得到它。

如何解决基础错误,以便我可以解析NSErrordomaincode属性?

我使用的是Swift 3和Alamofire 4.3.0。

2 个答案:

答案 0 :(得分:1)

查看response.result

if case let .failure(error) = response.result {
    let error = error as NSError
    print("\(error.domain)")
    print("\(error.code)")
    print("\(error.userInfo)")
}

答案 1 :(得分:1)

我知道答案有点晚了;-)。但是试试这个:

... } catch let error as NSError {
print("UnderlyingError: \(String(describing: error.userInfo[NSUnderlyingErrorKey]))")}