小编AsM*_*nas的帖子

当api请求失败时如何在swift alamofire中使用responsedecodable解析错误

我正在尝试使用 alamofire 和 responseDecodable 向后端发出 API 请求。

AF.request(Router.registerFacebookUser(facebookToken: token)).validate().responseDecodable(of: UserConfig.self) { result in
        switch result.result {
        case let .success(userConfig):
            onAuthentication(userConfig)
        case let .failure(error):
            print(error)
            //somehow get the message from ERROR JSON and pass it here
            onFailure(error.localizedDescription)
        }
    }
Run Code Online (Sandbox Code Playgroud)

当调用成功时,它成功地将 JSON 解析为模型。但是,在某些特殊情况下,它会失败。例如,如果用户已经注册,我会收到 JSON 响应:

{
   "error":{
      "message":"User already exist"
   }
}
Run Code Online (Sandbox Code Playgroud)

是否可以覆盖我们收到的 AF 错误?或者如果请求失败,也许可以解析另一个对象?或者还有其他方法可以访问错误消息吗?

swift alamofire

4
推荐指数
1
解决办法
2129
查看次数

标签 统计

alamofire ×1

swift ×1