【问题标题】:AWS API Gateway Error Handling with Generated SDK (Swift)使用生成的 SDK (Swift) 处理 AWS API Gateway 错误
【发布时间】:2017-08-07 04:07:17
【问题描述】:

我对@9​​87654323@ 和使用API Gateway -> Lambda -> RDS 还很陌生。另外,我在使用API 的iOS 移动应用程序中使用为Swift 生成的SDK。

返回成功 (200) 时,直接的路径可以正常工作。但是,我正在尝试为边缘情况添加更多错误处理。如果API 请求未找到预期的资源,我将尝试返回404 error。我已将其适当地添加到 API 集成响应和方法响应中。我可以对此进行测试,并按预期返回错误模型以及正确的HTTP Status code of 404。

但是,我正在努力解决如何在移动应用程序端处理这个问题。生成的SDK 将如何处理?它只是在 API 调用中引发错误,我希望有一种方法可以检索我定义的“错误”模型对象。

Here is normal path

Here is failed path with 404

生成的 SDK (Swift) 方法:

public func userUseridentityGet(useridentity: String) -> AWSTask<RSAPI_UserModel> {
let headerParameters = [
    "Content-Type": "application/json",
    "Accept": "application/json",           
]

let queryParameters:[String:Any] = [:]

var pathParameters:[String:Any] = [:]
pathParameters["useridentity"] = useridentity

return self.invokeHTTPRequest("GET", urlString: "/user/{useridentity}", pathParameters: pathParameters, queryParameters: queryParameters, headerParameters: headerParameters, body: nil, responseClass: RSAPI_UserModel.self) as! AWSTask<RSAPI_UserModel>
}

在运行时,当我打印出错误时,我会注销以下内容:

发生错误:错误 域=com.amazonaws.AWSAPIGatewayErrorDomain 代码=1“(空)” 用户信息={HTTPBody={ 代码 = 404; message = "未找到结果。"; “请求ID”=“132b8eaa-7b24-11e7-b1fd-d342f0413b7d”; 类型=未找到; }, HTTPHeaderFields={type = immutable dict, count = 8, entries => 0 : x-cache = {contents = "错误 来自云端"} 1 : Content-Type = {contents = "application/json"} 3 : x-amzn-requestid = {内容= “131a5075-7b24-11e7-87bd-9fcb4cb4e04e”} 4:通过 = {内容 = “1.1 bd4761ff0774f9ee778140b91a0431c9.cloudfront.net (CloudFront)"} 6: 日期 = {内容 = "星期一,8 月 7 日 2017 03:54:13 GMT"} 10:内容长度 = 134 11:x-amzn-trace-id = {内容= “采样=0;根=1-5987e464-d3d8f7801b7ae5aa6a52fc1b”} 12: x-amz-cf-id = {内容 = "QKpl64W1qDaeo0zlsx2iOwTW0oO_jyPRmMT7ByPKLnen04qiPEeD6w=="} } }

问题是如何将其反序列化到我定义的“错误”对象模型中?如何正确检测此错误情况,以便在我的移动应用程序中编写逻辑来处理它?

【问题讨论】:

    标签: swift aws-api-gateway


    【解决方案1】:

    作为一种解决方法,我刚刚通过手动提取数据来解决这个问题。如果有人有更好的方法来处理这个问题。我肯定会感兴趣的。

    if let error = task?.error as NSError? {
        print("Error occurred: \(error)")
        if error.code == 1 {
            let httpBody : NSDictionary = (error.userInfo["HTTPBody"] as? NSDictionary)!
            print("Code \(httpBody["code"] ?? "none") - Type \(httpBody["type"] ?? "none") - Message \(httpBody["message"] ?? "none") - RequestID \(httpBody["request-id"] ?? "none")")
        }
        return nil
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-08
      • 2018-01-15
      • 1970-01-01
      • 2018-02-26
      • 2022-06-17
      • 2017-06-23
      相关资源
      最近更新 更多