如何从 Alamofire 错误中获取底层错误?

How do I get at the underlying Error from an Alamofire error?

对于此请求:

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

    return
  }
}

我在控制台中看到这个打印:

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(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 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)
  }
}

我认为有 switches、cases 和转换到和来自 BackendErrorAFErrorError、and/or NSError,但是我好像看不懂

我怎样才能找到潜在的错误,以便我可以解析 domaincodeuserInfo 属性?

我正在使用 Swift 3 和 Alamofire 4.3.0。

对于 Alamofire 4.3,查看 response.result:

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

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

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