gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:15:02 25 4
gpt4 key购买 nike

对于此请求:

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.})))

我想做的是找出潜在的错误,这样我就可以解析 domain , code , 和 userInfo属性。

我创建了 BackendError Alamofire 在 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 中的示例完全一样:

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)
}
}

我认为有switch是的,case s,并转换为 BackendError , AFError , Error , 和/或 NSError , 但我似乎无法理解。

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

我正在使用 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)")
}

关于ios - 如何从 Alamofire 错误中获取底层错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42591945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com