简体   繁体   English

使用 node.js 从 AWS Lambda 将自定义错误传回 AWS API Gateway

[英]Passing custom errors back to AWS API Gateway from AWS Lambda using node.js

I'd really like to be able to pass custom errors back out of my Lambda function via the API Gateway.我真的很希望能够通过 API 网关将自定义错误从我的 Lambda 函数传回。

I'm generating the errors this way:我以这种方式生成错误:

if (error) { // failure

        APIGatewayResult = { // set error object
            statusCode: 608,
            message: 'File upload to buffer failed.',
            error: error
        };
        done();

    };

I'm fairly certain the format above is either parsed incorrectly or I can't pass it back out like this:我很确定上面的格式要么解析不正确,要么我不能像这样传回:

done = (err, res) => callback(null, {
        statusCode: err ? APIGatewayResult.statusCode : APIGatewayResult.statusCode,
        body: err ? JSON.stringify(APIGatewayResult) : JSON.stringify(APIGatewayResult),
        headers: {
            'Content-Type': 'application/json',
        },
});

I'd like the response I get from API gateway to look like this:我希望从 API 网关得到的响应如下所示:

{
    "statusCode": 608,
    "message": "File upload to buffer failed.",
    "error": {}
}

Instead of this:取而代之的是:

{
    "message": "Internal server error"
}

There are two parts to your question:你的问题有两个部分:

1) How to handle Lambda error messages in API Gateway: 1) API Gateway 中如何处理 Lambda 错误信息:

There are numerous guides available which explain how to parse Lambda error responses in API Gateway to return them in the desired format to consumers.有许多可用的指南解释了如何在 API Gateway 中解析 Lambda 错误响应以将它们以所需的格式返回给消费者。 For example: Error Handling Patterns in Amazon API Gateway and AWS Lambda .例如: Amazon API Gateway 和 AWS Lambda 中的错误处理模式

Important to note that you're parsing null to the first parameter of the callback function from your lambda.需要注意的是,您正在将 lambda 中的回调函数的第一个参数解析为null The first parameter is the error response message, so by providing it as null the Lambda will be returning a successful 200 response back to API Gateway.第一个参数是错误响应消息,因此通过将其提供为 null,Lambda 将向 API Gateway 返回成功的 200 响应。

2) How to override the generic unhandled exception message in API Gateway: 2) 如何覆盖 API Gateway 中的通用未处理异常消息:

This is required because as mentioned in the comments, the error you're receiving appears to have been thrown due to an unhandled exception in your application.这是必需的,因为如评论中所述,您收到的错误似乎是由于应用程序中未处理的异常而引发的。 You'll need to review the logs to identify the source of the issue.您需要查看日志以确定问题的根源。

But to change the format of the default error response object you'll need to add a custom Gateway response in API Gateway .但是要更改默认错误响应对象的格式,您需要在 API Gateway 中添加自定义网关响应

It's difficult to offer a more concrete example given the limited information provided but this should be enough to point you in the right direction to find what you're looking for.鉴于所提供的信息有限,很难提供更具体的示例,但这应该足以为您指明正确的方向以找到您正在寻找的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Lambda 与 API 网关和 Node.js 返回 HTTP 50 - AWS Lambda with API gateway and Node.js returning HTTP 502 如何从AWS Lambda Node.js 8.10异步函数向AWS API网关返回net.socket数据? - How to return net.socket data from AWS Lambda Node.js 8.10 async function to AWS API gateway? 使用 Node.JS 监控 AWS Api 网关 - Monitoring AWS Api Gateway Using Node.JS AWS API 网关与 Lambda HTTP GET 请求(Node.js)502 错误网关 - AWS API Gateway with Lambda HTTP GET Request (Node.js) 502 Bad Gateway 将Node.js API与AWS Lambda混合 - Mixing Node.js API with AWS Lambda 使用AWS Lambda和Node.js在API端点上显示图像 - Displaying an image on API endpoint using aws lambda and node.js 如何使用Javascript(Node.js)读取通过Http Post发送的变量值并在AWS API Gateway Lambda上获取? - How to read variable value sent over Http Post and Get on AWS API Gateway Lambda using Javascript(Node.js)? AWS Lambda/API 网关 - 传递 ID 以使用 node.js 在 mysql 中删除一行 - AWS Lambda/API Gateway - Pass an ID to delete a row in mysql with node.js AWS + API网关+ Lambda + Node.js在Google上的操作ApiAiApp - AWS + API Gateway + Lambda + Node.js actions-on-google ApiAiApp 如何解析AWS Lambda(node.js)中通过AWS Api网关上传的数据的内容类型? - How to parse the Content-type of the data being uploaded through aws Api gateway in aws Lambda (node.js)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM