来自带有 Lambda 的 API 网关的无服务器离线自定义错误

Rub*_*san 3 aws-lambda serverless-framework

有没有办法从 API 网关返回自定义错误对象和状态代码?我获得了 200 状态。

 var response = {
    status: 400,
    errors: [
       {
          code:   "226",
          message: "Password and password confirmation do not match."
        }
    ]
 }

context.done(JSON.stringify(response));
Run Code Online (Sandbox Code Playgroud)

jen*_*ter 5

如果要响应错误,则必须使用带有错误响应构造的成功回调。

如果您使用 context.fail() 回调,AWS 将假定 Lambda 技术上失败并使用 API 网关中存在的默认映射进行响应。

示例错误响应:

'use strict';

module.exports.hello = (event, context, callback) => {
  const response = {
    statusCode: 400,
    body: JSON.stringify({
      errors:[{
        code: "226",
        message:"Password confirmation do not match"
      }]
    }),
  };
  context.done(null, response);
};
Run Code Online (Sandbox Code Playgroud)