因此,当您从scracth创建新的lambda时,将获得以下默认index.js内联代码:
(Remi_Brun)(_.+)([a-zA-Z0-9-_]{11}.en.vtt)
此lambda由api网关端点使用。如果某些情况不太正确,返回错误代码的正确方法是什么?网关会处理它还是只将上面的状态更改为新的错误代码?
答案 0 :(得分:0)
1xx (Informational): The request was received, continuing process
2xx (Successful): The request was successfully received, understood, and accepted
3xx (Redirection): Further action needs to be taken in order to complete the request
4xx (Client Error): The request contains bad syntax or cannot be fulfilled
5xx (Server Error): The server failed to fulfill an apparently valid request
这只是返回200响应,这是成功的响应。您只需要根据您的实现返回正确的代码即可。我想这就是您要的。
有关状态码的更多信息,您可以阅读此Wiki。 https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
但是由于您在询问实施问题,因此我将执行以下操作
exports.handler = async (event) => {
// TODO implement
let statusCode = 200;
let message = 'success';
... // do stuff
statusCode = 400 // something went wrong
const response = {
statusCode: statusCode,
body: JSON.stringify(message)
};
return response;
};
取决于您希望应用程序如何扩展,这完全取决于您。请记住,永远不要重复代码。保持模块化。
此外,您还可以在函数中声明
// Somewhere else
response = function (statusCode, message) {
return {
statusCode: statusCode,
body: JSON.stringify(message)
};
};
exports.handler = async (event) => {
// TODO implement
let statusCode = 200;
let message = 'success';
... // do stuff
statusCode = 400 // something went wrong
return response(statusCode, message);
};
从AWS中检出错误处理模式。 除非您已经阅读过,否则这将是一个不错的开始。 https://aws.amazon.com/blogs/compute/error-handling-patterns-in-amazon-api-gateway-and-aws-lambda/
基本上,您需要满足以下条件
exports.handler = (event, context, callback) => {
var myErrorObj = {
errorType : "InternalServerError",
httpStatus : 500,
requestId : context.awsRequestId,
message : "An unknown error has occurred. Please try again."
}
callback(JSON.stringify(myErrorObj));
};