Lambda 始终连接到 appsync returns Lambda:Unhandled errorType,无论自定义异常如何

Lambda connected to appsync always returns Lambda:Unhandled errorType no matter the custom Exception

我有一个将消息存储到 dynamodb 中的聊天 lambda。如果用户无权添加消息,我需要 return 向我的客户端发送具有唯一错误类型和消息的自定义错误异常。

我已经使用 https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-lambda.html

中的文档设置了我的自定义错误

即:

export default class ForbiddenError extends Error {
    /**
     * ForbiddenError constructor
     * @param {string} name
     * @param {string} message
     */
    constructor(name = null, message = null) {
        message = message || 'You are forbidden from performing this action.'
        super(message)
        this.name = name || 'Forbidden'
    }
}

然后我通过以下方式在我的应用程序中抛出错误:

throw new ForbiddenError()

当我在本地测试我的 lambda 时,一切正常,代码遇到错误。我抓住它并打电话给

context.fail(error)

即使我在 AWS 控制台中使用测试 lambda 测试我的 lambda,我也会收到包含消息和错误类型的漂亮响应:

class ForbiddenError extends Error {
    constructor(message) {
        super(message)
        this.name = 'ForbiddenError'
    }
}

exports.handler = (event, context, callback) => {
    throw new ForbiddenError('You are forbidden from performing this action.')
    
    return context.fail('test');
};

和错误:

{
  "errorType": "ForbiddenError",
  "errorMessage": "You are forbidden from performing this action.",
  "trace": [
    "ForbiddenError: You are forbidden from performing this action.",
    "    at Runtime.exports.handler (/var/task/index.js:9:21)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}

但是当我使用 appsync 调用我的 lambda 时,突然只有消息被传递到错误中,但是 errorType 总是相同的:Lambda:Unhandled 即:

{
          "graphQLErrors": [
                    {
                              "path": [
                                        "storeStreamChatMessage"
                              ],
                              "data": null,
                              "errorType": "Lambda:Unhandled",
                              "errorInfo": null,
                              "locations": [
                                        {
                                                  "line": 2,
                                                  "column": 3,
                                                  "sourceName": null
                                        }
                              ],
                              "message": "You are forbidden from performing this action."
                    }
          ],
          "networkError": null,
          "message": "GraphQL error: You are forbidden from performing this action."
}

响应映射模板与文档中的相同:

  ResponseMappingTemplate: |
    #if($ctx.error)
      $util.error($ctx.error.message, $ctx.error.type)
    #end

    $util.toJson($context.result)

我试图将 $ctx.error.type 更改为 $ctx.error.errorType,但错误类型 returned 为“自定义 lambda 错误”而不是“禁止”。

我已经尝试使用 lambda exports.handler 中的 context.fail() 和回调方法,两者在控制台中都运行良好,但都只有 return 从应用同步解析器。

我已经测试过 context.fail(error) 确实在 lambda 中被调用,并且在最终调用 context.fail(error)[= 之前​​通过 .catch() 语句捕获了异常。 21=]

即使是 cloudwatch 在调用主 lambda 时也会显示带有消息和错误类型的错误,所以我怀疑 exepction 和 context.fail() returning

中的错误

最后,我将错误字符串化并通过消息栏传递给了我的客户。不幸的是,当我设法通过解析器响应映射模板传递正确的错误类型时,returned 错误开始 returning “自定义错误”作为 return 错误类型。