Hey all im running some code in the lambda console and getting the following error:
{
"errorMessage": "'message'",
"errorType": "KeyError",
"requestId": "47445356-38a2-43ec-98f5-2dde22c6af1f",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 11, in lambda_handler\n dynamodb.pushPriceHistoryToDynamo(str(i.name))\n",
" File \"/opt/python/dynamodbPushData.py\", line 17, in pushPriceHistoryToDynamo\n response = self.newTable.createPriceHistoryTable(tableName)\n",
" File \"/opt/python/createNewTable.py\", line 37, in createPriceHistoryTable\n print(f\"[createNewTable]: {err.response['message']} Table already created\")\n"
]
}
This is an exception I have caught in my code on purpose however it is non-fatal and should still continue. That being said I went ahead and add a "pass" in the exception but it is still erroring out below is the code block its talking about:
def createPriceHistoryTable(self, tableName: str) -> int:
try:
response = self.dynamodb.create_table(
TableName=tableName,
KeySchema=[
{
'AttributeName': 'date',
'KeyType': 'HASH' # Hash Key
}
],
AttributeDefinitions=[
{
'AttributeName': 'date',
'AttributeType': 'S' # string data type
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 50
}
)
print("[createNewTable]: New table created")
return 200
except ClientError as err:
if err.response['ResponseMetadata']['HTTPStatusCode'] == 400:
print(f"[createNewTable]: {err.response['message']} Table already created")
else:
print(f"[createNewTable]: {err.response['message']}")
pass
return err.response['ResponseMetadata']['HTTPStatusCode']
Here is the lambda function I am actually running:
import json
import dynamodbPushData
import RegionIdEnum
import ItemIdEnum
import time
def lambda_handler(event, context):
dynamodb = dynamodbPushData.pushData()
for i in list(ItemIdEnum.item):
dynamodb.pushPriceHistoryToDynamo(str(i.name))
# dynamodb.pushItemOrdersToDynamo(str(i.name))
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Edit: Commenting out the prints and leaving the pass in does fix it. That being said I would like to log proper information for debugging. Should i use a logger instead?
except ClientError as err:
# if err.response['ResponseMetadata']['HTTPStatusCode'] == 400:
# print(f"[createNewTable]: {err.response['message']} Table already created")
# else:
# print(f"[createNewTable]: {err.response['message']}")
pass
return err.response['ResponseMetadata']['HTTPStatusCode']
except KeyError:
# if err.response['ResponseMetadata']['HTTPStatusCode'] == 400:
# print(f"[createNewTable]: {err.response['message']} Table already created")
# else:
# print(f"[createNewTable]: {err.response['message']}")
pass
return err.response['ResponseMetadata']['HTTPStatusCode']
Here's an example from the docs of the data structure that the ClientError
contains:
{
'Error': {
'Code': 'SomeServiceException',
'Message': 'Details/context around the exception or error'
},
'ResponseMetadata': {
'RequestId': '1234567890ABCDEF',
'HostId': 'host ID data will appear here as a hash',
'HTTPStatusCode': 400,
'HTTPHeaders': {'header metadata key/values will appear here'},
'RetryAttempts': 0
}
}
To access the error message you'd need to use:
# Message with Capital M
err.response["Error"]["Message"]
You're trying to access a non-existent key in your print statements, thus the KeyError
.