对于此请求:
Alamofire.request("google").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("google").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提供,例如,在 github/Alamofire/Alamofire#handling-errors :
I created the BackendError enum that Alamofire provides as an example at github/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) }并且我还实现了示例通用响应对象序列化与 github/Alamofire/Alamofire #generic-response-object-serialization :
and I also implemented the example generic response object serialization exactly like the example at github/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(BackendErrorwork(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错误中找出潜在的错误?
发布评论