title: How to throw HTTP error code with AWS Lambda using Lambda Proxy?
body: <p>I created an AWS Lambda function using Python 3.8 with a <em>Lambda Proxy</em> API Gateway trigger:
  <a href="https://i.stack.imgur.com/t01l1.png" rel="noreferrer"><img src="https://i.stack.imgur.com/t01l1.png" alt="API Gateway"></a></p>
  
  <p>It is indeed possible to return custom HTTP error codes:</p>
  
  <pre class="lang-py prettyprint-override"><code>def lambda_handler(event, context):
      return {
          'statusCode': 400,
          'body': json.dumps('This is a bad request!')
      }
  </code></pre>
  
  <p>However, some online examples (e.g. <a href="https://aws.amazon.com/blogs/compute/error-handling-patterns-in-amazon-api-gateway-and-aws-lambda/" rel="noreferrer">1</a>, <a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/handle-errors-in-lambda-integration.html#handle-standard-errors-in-lambda-integration" rel="noreferrer">2</a>) simply raise an Exception to return the error with a custom message. If an Exception is thrown on the handler as below, the server return a 502 as the response is <a href="https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/" rel="noreferrer">not in the expected format for Proxy Integration</a>:</p>
  
  <pre class="lang-py prettyprint-override"><code>def lambda_handler(event, context):
      raise Exception('This is an exception!')
  </code></pre>
  
  <p>I think the examples rely on some Integration Response templates and do not use the Proxy Integration. Is it possible to achieve the same thing with Lambda Proxy? I'd like to avoid to globally catch exceptions in the Lambda handler to build custom error responses.</p>