如何提取在 Moya 中失败的请求的 url

Posted

技术标签:

【中文标题】如何提取在 Moya 中失败的请求的 url【英文标题】:How do I extract url of a request that failed in Moya 【发布时间】:2020-03-26 14:09:33 【问题描述】:
    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 是从哪里来的。我必须将响应映射到一个结构中(如果需要,可以使该结构可编码)

【问题讨论】:

【参考方案1】:

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

    

【讨论】:

我以某种方式在服务器的 404 上以 .success 结尾。莫亚虫? 不是 moya bug 只是在 TargetTypevalidationType 默认值是 none 只是让它成功代码 var validationType: ValidationType get

以上是关于如何提取在 Moya 中失败的请求的 url的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Moya 框架重试请求

如何在 moya 中通过 POST 请求传递 JSON 正文

我们如何使用 Moya 调试/查看通过 API 设置的请求?

使用 Moya 处理缓存

RxSwift+Moya+Moya_ObjectMapper+MJRefresh,刷新失败?

如何在 Moya 中传递 URLRequest