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"))
}
}那么mapObject是从哪里来的呢?我要将响应映射到一个结构中(如果需要,可以使该结构可编码)
发布于 2020-03-26 22:32:42
moya的回应是Result<Moya.Response, MoyaError>
在failure中,您有一个MoyaError对象,即Enum,您只需使用switch - case即可获得所有错误选项
// 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)
}所以你可以像那样处理moya错误
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
}
}https://stackoverflow.com/questions/60869174
复制相似问题