如何从Alamofire错误中找出潜在的错误?

编程入门 行业动态 更新时间:2024-10-22 02:51:57
本文介绍了如何从Alamofire错误中找出潜在的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于此请求:

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错误中找出潜在的错误?

本文发布于:2023-11-26 18:03:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634613.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!