Ios 如何提取在Moya中失败的请求的url
mapObject来自哪里。我将响应映射到一个结构中(如果需要,可以使该结构可编码)moya的响应是Ios 如何提取在Moya中失败的请求的url,ios,moya,Ios,Moya,mapObject来自哪里。我将响应映射到一个结构中(如果需要,可以使该结构可编码)moya的响应是结果 在failure中,您有MoyaError对象,它是Enum,您可以使用switch-case获得所有错误选项 provider.request(.getRoot) { result in switch result { case .success(let response): print ("root \(response)"
结果
在failure
中,您有MoyaError
对象,它是Enum
,您可以使用switch-case
获得所有错误选项
provider.request(.getRoot) { result in
switch result {
case .success(let response):
print ("root \(response)")
// let response = try? response.mapObject(FolderResponse.self)
// print ("root \(response) \(response)")
case .failure(let error):
let r = result.0.request how do I get the request url from this context???
^^^^^^^^^^^^^^^^^^
print("BaseURL: \(r)" + (error.errorDescription ?? "Unknown error"))
}
}
所以你可以像那样处理moya的错误
// A type representing possible errors Moya can throw.
public enum MoyaError: Swift.Error {
/// Indicates a response failed to map to an image.
case imageMapping(Response)
/// Indicates a response failed to map to a JSON structure.
case jsonMapping(Response)
/// Indicates a response failed to map to a String.
case stringMapping(Response)
/// Indicates a response failed to map to a Decodable object.
case objectMapping(Swift.Error, Response)
/// Indicates that Encodable couldn't be encoded into Data
case encodableMapping(Swift.Error)
/// Indicates a response failed with an invalid HTTP status code.
case statusCode(Response)
/// Indicates a response failed due to an underlying `Error`.
case underlying(Swift.Error, Response?)
/// Indicates that an `Endpoint` failed to map to a `URLRequest`.
case requestMapping(String)
/// Indicates that an `Endpoint` failed to encode the parameters for the `URLRequest`.
case parameterEncoding(Swift.Error)
}
不知怎的,我在404服务器上获得了成功。Moya bug?不是Moya bug只是在
TargetType
有validationType
默认值是none
只要使它成功即可CodeVar validationType:validationType{get}
provider.request(.getRoot) { result in
switch result {
case .success(let response):
print ("root \(response)")
// let response = try? response.mapObject(FolderResponse.self)
// print ("root \(response) \(response)")
case .failure(let error):
self.handleMoyaError(error)
}
}
// here you canc heck all of this error
private func handleMoyaError(_ moyaError : MoyaError){
switch moyaError {
case let .statusCode(response):
print(response.request?.url)
case .underlying(let nsError as NSError, let response): break
// nsError have URL timeOut , no connection and cancel request
// just use response to map of there is error
default: break
}
}