如何从Alamofire错误中找出潜在的错误? [英] How do I get at the underlying Error from an Alamofire error?

查看:138
本文介绍了如何从Alamofire错误中找出潜在的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于此请求:

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代码= 3840字符
0周围的无效值。 UserInfo = {NSDebugDescription =字符0周围的无效值。}))))))

Optional(my_app_name.BackendError.jsonSerialization(Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 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(错误
域= NSCocoaErrorDomain代码= 3840字符
0周围的无效值。 UserInfo = {NSDebugDescription =字符0周围的无效值。))))

jsonSerialization(Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))

我要尝试的是获取基本错误,以便我可以解析代码 userInfo 属性。

What I'm trying to do is get at the underlying error so I can parse the domain, code, and userInfo properties.

我创建了 BackendError 枚举,该枚举由Alamofire提供,例如,在 https://github.com/Alamofire/Alamofire#handling-errors

I created the BackendError enum that Alamofire provides as an example at https://github.com/Alamofire/Alamofire#handling-errors :

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 :

and I also implemented the example generic response object serialization exactly like the example at 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)
  }
}

我认为有个开关 es, case s,并强制转换为和来自 BackendError AFError Error 和/或 NSError ,但我似乎无法理解。

I think there are switches, cases, and casts to and from BackendError, AFError, Error, and/or NSError, but I can't seem to get it.

如何获取基本错误这样我就可以解析代码 userInfo 属性?

How can I get at the underlying error so I can parse the domain, code, and userInfo properties?

我正在使用Swift 3和Alamofire 4.3.0。

I'm using Swift 3 and Alamofire 4.3.0 .

推荐答案

查看 response.result

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

这篇关于如何从Alamofire错误中找出潜在的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆