Error Handling¶
Our API Gateway is now working if you pass the correct parameters into it. The next question is what happens if someone does not pass the proper parameters in or even no parameters at all? Sadly, we get a really nasty error message coming back. This is OK, but really we should be nice programmers and handle it better. What we will do is alter our Lambda code to trap these kinds of errors.
Tasks:
- use try and except to catch wrong or no parameters being passed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | def lambda_handler(event, context):
# function returns a row from our chocolate_user DynmamoDB
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('chocolate_user')
try:
response = table.get_item(
Key = {
'email':event['email_address']
}
)
try:
result = response['Item']
result = replace_decimals(result)
except:
result = {}
print(result)
return_var = {
'statusCode': 200,
'body': json.dumps(result)
}
return return_var
except:
return {
'statusCode': 204,
'body': json.dumps({})
}
|