【问题标题】:How to generate or simulate Internal Server error (500) on aws lambda function?如何在 aws lambda 函数上生成或模拟内部服务器错误 (500)?
【发布时间】:2022-09-28 21:48:29
【问题描述】:

我想在我的 lambda 函数上发生内部服务器错误 ( 5xx/500 ) 事件时测试我的代码功能和错误日志。

我不想使用 localstack 或一些模拟库在本地执行此操作,我想在 AWS 上部署的 lambda 函数上尝试此操作。

更新 :-事件类型为APIGatewayProxyEvent

处理事件的函数:-

functionFind = async (
    event: APIGatewayProxyEvent
  ): Promise<APIGatewayProxyResult> => {
    try {
      this.loggingLambdaEvent(event)
      const request = new functionRequest(event)
      await ValidationHelper.validateOrReject(request)
      const useCaseOutput = await this.useCase.execute(request)
      return new functionResponseBuilder(useCaseOutput).serialize()
    } catch (error) {
      if (
        error instanceof InvalidRequestParameter ||
        error instanceof AccountInvalidParameter
      ) {
        return new BadRequestResponse(error).serialize()
      } else if (
        error instanceof AccountIdNotFound ||
        error instanceof AccountInvalidToken
      ) {
        return new functionAndBearerTokenUnauthorizedResponse(
          error
        ).serialize()
      } else {
        return new InternalServerErrorResponse(error).serialize()
      }
    }
  }

有没有办法这样做?

    标签: typescript amazon-web-services aws-lambda aws-serverless


    【解决方案1】:

    看起来 AWS 有 Fault Injection Simulator 来处理这些事情。不幸的是,目前这项服务非常有限。

    我了解您对为此使用 LocalStack 不感兴趣。但是 LocalStack 最近有了自己的 FIS 实现。在那里,您可以模拟任何 API 调用的错误。如果您愿意考虑,请查看 here 以了解它是如何完成的。

    【讨论】:

      猜你喜欢
      • 2020-07-07
      • 2021-06-09
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 2014-07-02
      • 2019-12-21
      • 1970-01-01
      相关资源
      最近更新 更多