简体   繁体   English

Alamofire+Combine:如何从 AFError 中获取自定义错误类型

[英]Alamofire+Combine: how to get custom error type out of AFError

I'm using Alamofire and its Combine helpers to do a simple network request as follows:我正在使用 Alamofire 和它的 Combine 助手来做一个简单的网络请求,如下所示:

class AlamofireClient {
    func getData<T: Decodable>(request: URLRequestConvertible) -> AnyPublisher<T, APIError> {
         AF.request(request)
            .validate()
            .publishDecodable(type: T.self)
            .value()
            .mapError(APIError.init(error:))
            .eraseToAnyPublisher()
    }
}

enum APIError: Error {
    init(error: AFError) {
        self = .network
    }
    
    case network
    case custom(Int, String, String)
    
    var message: String {
        switch self {
        case .network:
            return "Network Error"
        case .custom(let code, let message, let technicalMessage):
            return "Custom error info are: \(code) -- \(message) -- \(technicalMessage)"
        }
    }
}

I'm stuck at extracting the custom error info that is returned from the server, which's in my case has 3 items: code, message and technicalMessage.我一直在提取从服务器返回的自定义错误信息,在我的情况下有 3 个项目:代码、消息和技术消息。

So i had worked over similar situation where i wants to pass my own Error, instead of Alamofire.所以我在类似的情况下工作过,我想通过我自己的错误,而不是 Alamofire。 Please see the below code implementation i used, its working for me.请参阅我使用的以下代码实现,它对我有用。 Hopefully it will help some one too.希望它也能帮助一些人。

 public func request<T>(response: @escaping (AnyPublisher<T, MyNetworkError>) -> ()) where T : Decodable {
        response(
        MyNetwork.shared.sendRequest(path: “sample/url/endpoints”)
            .validate()
            .publishDecodable(type: T.self)
            .value()
            .mapError{MyNetworkError.networkError(error: ($0 as Error))}
            .eraseToAnyPublisher())
    }

While my own error looks like below.虽然我自己的错误如下所示。

public enum MyNetworkError: Error {
    case networkError(error: Error)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 5 &amp; Alamofire 5:GET 方法错误:Alamofire.AFError.URLRequestValidationFailureReason.bodyDataInGETRequest(22 bytes) - Swift 5 & Alamofire 5 : GET method ERROR: Alamofire.AFError.URLRequestValidationFailureReason.bodyDataInGETRequest(22 bytes) 错误:可选(Alamofire.AFError.invalidURL(“”)) - Error: Optional(Alamofire.AFError.invalidURL(“”)) “结果”类型的 Alamofire 值<any, aferror> ' 没有成员 'isSuccess' (Swift 5)</any,> - Alamofire Value of type 'Result<Any, AFError>' has no member 'isSuccess' (Swift 5) Alamofire 5:“结果”类型的值<data, aferror> '没有成员'值'</data,> - Alamofire 5: Value of type 'Result<Data, AFError>' has no member 'value' Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域=NSCocoaErrorDomain 代码=3840 - Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 操作无法完成。 (Alamofire.AFError 错误 9。) - The operation could not complete. (Alamofire.AFError error 9.) 如何从 AFError 中确定特定的 409 错误? - how to determine the specific 409 error from an AFError? Alamofire:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength) - Alamofire: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength) Alamofire - Alamofire.AFError.responseSerializationFailed - Xcode 8 - Alamofire - Alamofire.AFError.responseSerializationFailed - Xcode 8 '结果类型的值<any, aferror> ' 没有成员 '值' (使用 Xcode 13.2 和 AlamoFire 5.4.3)</any,> - Value of type 'Result<Any, AFError>' has no member 'value' (with Xcode 13.2 and AlamoFire 5.4.3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM