简体   繁体   English

如何在部署在 aws lambda 上并与 API 网关集成的 python 代码中隐藏我的异常堆栈跟踪

[英]How can I hide my exception stack trace in python code which is deployed on aws lambda and integrated with API gateway

I use raise Exception (" message ") and it returns this on browser {"errorMessage": "{"httpStatus": 200, "message": "Exception: {\"httpStatus\": 200, \"message\": \"Exception: [InternalServerError] Project not found\"}"}", "errorType": "Exception", "stackTrace": [the stack trace]}我使用 raise Exception (" message ") 它在浏览器上返回 {"errorMessage": "{"httpStatus": 200, "message": "Exception: {\"httpStatus\": 200, \"message\": \"Exception: [InternalServerError] Project not found\"}"}", "errorType": "Exception", "stackTrace": [堆栈跟踪]}

The stack trace causes security issue堆栈跟踪导致安全问题

If you are using API gateway in front of lambda.如果您在 lambda 前面使用 API 网关。 You can set reponse mapping template like below, this will override the response error message and response code as well.您可以像下面这样设置响应映射模板,这也将覆盖响应错误消息和响应代码。

#if($inputRoot.toString().contains('InternalServerError'))
{
  "message": "Internal Server Error"
}
#set($context.responseOverride.status = 500)

Alternately you can also catch all the exceptions in the lambda and return whatever you like.或者,您也可以捕获 lambda 中的所有异常并返回您喜欢的任何内容。 However this will not override the status code and you would still get 200 even in case of error.但是,这不会覆盖状态代码,即使出现错误,您仍然会得到 200。

def handler(event, context):
   try:
       dosomething(event)
   except Exception:
       retunrn { "message": "Internal Server error" }

def dosomething(event):
      .... You business logic.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在与 API 网关集成的 AWS Lambda python 中执行 302 重定向? - How to perform 302 Redirect in AWS Lambda python integrated with API Gateway? AWS xray + lambda 异常处理,如何返回错误消息并将我的 xray 跟踪标记为失败 - AWS xray + lambda exception handling, how can I return an error message and have my xray trace flagged as a failure 如何在 AWS Lambda 上使用 Python 从 Zappa 获取堆栈跟踪 - How do I get a stack trace from Zappa with Python on AWS Lambda 如何使用 Python Lambda 通过 API 网关提供 .zip? - How can I serve a .zip through an API Gateway with a Python Lambda? 在 AWS-Lambda(Python) 中,我如何调用需要在 header 中进行令牌授权的外部 API - In AWS-Lambda(Python), how can I Invoke External API which needs Token Authorization in the header 如何以编程方式检查Python中异常的堆栈跟踪? - How can you programmatically inspect the stack trace of an exception in Python? 如何在我的 Python 代码中跟踪语法错误? - How can I trace a syntax error in my Python code? 如何从Python中的异常对象获取堆栈跟踪? - How do I get the stack trace from an Exception Object in Python? JSON 有效负载,AWS Lambda(Python),API 网关 - JSON Payload, AWS Lambda (Python), API Gateway AWS API Gateway 和 Python Lambda 返回 HTML - AWS API Gateway and Python Lambda returning HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM