gpt4 book ai didi

swift - 使用生成的 SDK 处理 AWS API Gateway 错误 (Swift)

转载 作者:行者123 更新时间:2023-11-28 15:23:57 24 4
gpt4 key购买 nike

我对 AWS 和使用 API Gateway -> Lambda -> RDS 还很陌生。此外,我在使用 APIiOS 移动应用程序中使用为 Swift 生成的 SDK

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

但是,我正在努力解决如何在移动应用程序方面处理这个问题。生成的 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>
}

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

Error occurred: Error Domain=com.amazonaws.AWSAPIGatewayErrorDomain Code=1 "(null)" UserInfo={HTTPBody={ code = 404; message = "No Result Found."; "request-id" = "132b8eaa-7b24-11e7-b1fd-d342f0413b7d"; type = NotFound; }, HTTPHeaderFields={type = immutable dict, count = 8, entries => 0 : x-cache = {contents = "Error from cloudfront"} 1 : Content-Type = {contents = "application/json"} 3 : x-amzn-requestid = {contents = "131a5075-7b24-11e7-87bd-9fcb4cb4e04e"} 4 : Via = {contents = "1.1 bd4761ff0774f9ee778140b91a0431c9.cloudfront.net (CloudFront)"} 6 : Date = {contents = "Mon, 07 Aug 2017 03:54:13 GMT"} 10 : Content-Length = 134 11 : x-amzn-trace-id = {contents = "sampled=0;root=1-5987e464-d3d8f7801b7ae5aa6a52fc1b"} 12 : x-amz-cf-id = {contents = "QKpl64W1qDaeo0zlsx2iOwTW0oO_jyPRmMT7ByPKLnen04qiPEeD6w=="} } }

问题是如何将其反序列化到我定义的“错误”对象模型中?我如何适本地检测到这种错误情况,以便我可以在我的移动应用程序中编写逻辑来处理它?<​​/p>

最佳答案

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

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
}

关于swift - 使用生成的 SDK 处理 AWS API Gateway 错误 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45539281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com