Skip to content Skip to sidebar Skip to footer

Boto3 Python Lambda Customise Return Error

Is there a way to customise a Boto3 Lambda exception message in the form of a HTTP response and return it while also sending a forced failed? Here is an example except Exception a

Solution 1:

To get the custom error message as output for the lambda, you need to actually raise the exception with the custom error message.

except Exception as e:
  custom_error = '\nException : failed to invoke jobs.\n'
  custom_error += 'Error : ' + str(e) + '\n'raise Exception(custom_error)

And you will get an error message like:

{"errorMessage":"Exception : failed to invoke jobs. ....","stackTrace":[["/var/task/lambda_function.py",3,"my_always_fails_handler","raise Exception(custom_error)"]],"errorType":"Exception"}

You can find more on the AWS documentation that you can find here Python exceptions in lambdas

Solution 2:

You didn't mention it, but you're probably using AWS API Gateway to in front of your lambda.

You can use API Gateway "integration response" to transform your failed result to the HTTP response.

Here's a link to the docs

Post a Comment for "Boto3 Python Lambda Customise Return Error"