我通过以下命令创建了无服务器框架的功能:
sls function create some/api
然后创建了 schelton 代码:
'use strict';
module.exports.handler = function(event, context, cb) {
return cb({
message: 'Go Serverless! Your Lambda function executed successfully!'
});
};
并且,响应模板如下:
s-function.json
"responses": {
"400": {
"statusCode": "400"
},
"default": {
"statusCode": "200",
"responseParameters": {},
"responseModels": {
"application/json;charset=UTF-8": "Empty"
},
"responseTemplates": {
"application/json;charset=UTF-8": ""
}
}
}
但是,当我将错误对象返回给回调函数时cb(err, null)
,错误消息正确显示,但 statusCode 为 200。
如果我改为调用回调函数cb("400", err)
,则 statusCode 正确返回 400,但响应体不好:{"errorMessage":"400"}
。
有没有什么好的设置来显示机器人状态码(不仅是 400,还有 401,403,404,500...等等)和错误消息?