Ответ мойи: 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)
}
Так что вы можете просто справиться с моей ошибкой вот так
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
}
}