API Gateway WebSocket handle errors

时间:2019-05-31 11:23:34

标签: websocket aws-lambda aws-api-gateway

I'm setting a websocket app with API Gateway. I configured route responses for my lambda functions, so I'm able to handle these responses in my client side.

In my server, I return my response like this:

callback (null, {
  statusCode: 500,
  body: 'Some text.'
})

In my Websocket client side, the message event (onmessage) is triggered by this response.

The event.data contains the response body, due to lambda proxy integration response.

However, I can't find a way to trigger the error event (onerror), while my statusCode says that an error occured (500).

Do you have any solutions?

1 个答案:

答案 0 :(得分:0)

AWS Lambda函数回调具有以下结构:callback(error, result)

因此,对于错误,您可以将其称为callback(error),而对于成功的结果,您可以将其称为callback(null, result)

因此,您在应用程序上看到的是成功响应(状态码200),其中包含一些字段statusCode值为500的数据。因此,由于这是成功响应,因此不应触发{{1 }}回调,而只能是onerror

我的建议是,您还可以在onmessage方法和onmessage处理程序内处理除200之外的状态码响应,以解决不是从lambda函数捕获和检索到的错误(例如,AWS) Api网关问题,网络问题等。