bfi*_*ssa 1 node.js aws-lambda aws-api-gateway serverless-framework serverless
我正在编写供内部使用的API,并且这是我第一次使用无服务器框架。我正在Node.js中编写Lambda函数,并使用AWS API Gateway进行连接。
在某些情况下,我想返回一个自定义错误消息,而我试图编写一个允许我执行此操作的函数。现在,每当Lambda进程失败时,我都会从API收到一条标准消息。在代码中,如果我尝试使用杀死进程process.exit(1),即使我已经使用callback()以下命令返回了错误,也会收到一般错误:
{
"message": "Internal server error"
}
Run Code Online (Sandbox Code Playgroud)
如果不使用process.exit(1),我会callback()在日志中看到通过错误返回的错误,但是该过程继续进行,最终超时:
{
"message": "Endpoint request timed out"
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几种使用该callback()方法返回错误的方法,但是到目前为止,我还没有成功。我试过这种方法:
async function return_error(callback, context, error, returnCode){
console.error("FATAL ERROR: ", error);
let ErrorObj = {
errorType : "InternalServerError",
httpStatus : 500,
requestId : context.awsRequestId,
errorMessage : error
}
callback(JSON.stringify(ErrorObj));
process.exit(1);
}
Run Code Online (Sandbox Code Playgroud)
还有这个:
async function return_error(callback, error, returnCode){
console.error("FATAL ERROR: ", error);
callback({
isBase64Encoded: false,
statusCode: returnCode,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({"Error Message:": error})
}, null);
process.exit(1);
}
Run Code Online (Sandbox Code Playgroud)
(很抱歉,两者之间的较小语法更改。)
到目前为止,我还无法通过API向用户返回任何错误。我的错误总是被记录下来,并且该功能继续。任何帮助,将不胜感激。谢谢!
作为参考,我的serverless.yml文件的相关部分:
service: #Name of service
provider:
name: aws
runtime: nodejs8.10
role: #ARN of Iam role
functions:
screenshot:
handler: #Name of handler
timeout: 30
memorySize: 1280
reservedConcurrency: 10
events:
- http:
method: get
path: #path
contentHandling: CONVERT_TO_BINARY
authorizer:
type: aws_iam
plugins:
- serverless-plugin-chrome
- serverless-apigw-binary
- serverless-apigwy-binary
package:
exclude:
- node_modules/puppeteer/.local-chromium/**
custom:
apigwBinary:
types:
- '*/*'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3499 次 |
| 最近记录: |