问题描述
对于此请求:
Alamofire.request("https://google.com").responseCollection { (response: DataResponse<[User]>) in
guard response.result.isSuccess else {
print(response.error)
return
}
}
我在控制台中看到以下内容:
我尝试过的操作:
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)
以上打印内容:
我要尝试的是获取基本错误,以便我可以解析域
,代码
和 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提供,例如,在:
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 switch
es, case
s, 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错误中找出潜在的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!