books
at the endYou should see the function running normally and without any errors. Next, we will fix the code to make the function run error.
import json
import boto3
from decimal import *
from boto3.dynamodb.types import TypeDeserializer
client = boto3.client('dynamodb')
serializer = TypeDeserializer()
client_cloudwatch = boto3.client('cloudwatch')
class DecimalEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Decimal):
return str(obj)
return json.JSONEncoder.default(self, obj)
def deserialize(data):
if isinstance(data, list):
return [deserialize(v) for v in data]
if isinstance(data, dict):
try:
return serializer.deserialize(data)
except TypeError:
return {k: deserialize(v) for k, v in data.items()}
else:
return data
def lambda_handler(event, context):
try:
data_books = client.scan(
TableName='Book',
IndexName='name-index'
)
except Exception as e:
print(e)
format_data_books = deserialize(data_books["Items"])
for book in format_data_books:
try:
data_comment = client.query(
TableName="Books",
KeyConditionExpression="id = :id AND rv_id > :rv_id",
ExpressionAttributeValues={
":id": {"S": book['id']},
":rv_id": {"N": "0"}
}
)
format_data_comment = deserialize(data_comment['Items'])
book["comments"] = format_data_comment
except Exception as e:
print(e)
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method,X-Access-Token,XKey,Authorization"
},
"body": json.dumps(format_data_books, cls=DecimalEncoder)
}
try:
data_books = client.scan(
TableName='Book',
IndexName='name-index'
)
except Exception as e:
print(e)
The table name has been changed from Book and added try-except to catch errors
return {
"statusCode": 400,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method,X-Access-Token,XKey,Authorization"
},
"body": str(e)
}