Reputation: 10294
I am using DynamoDB
with AWS Lambda
.
I am writing about Updating data code, but I am in trouble.
export const updateData = async (event) => {
try{
const { email, uid } = JSON.parse(event.body);
const params = {
TableName: "MY_TABLE",
Key: { "email": email, "uid": uid },
UpdateExpression: "set updatedAt = :now",
ConditionExpression: "email = :email and uid = :uid",
ExpressionAttributeValues: {
":email": email, ":uid": uid, ":now": moment().unix()
},
ReturnValues: "ALL_NEW"
};
const data = await DYNAMO_DB.update(params, (err, data) => {
if(err) throw err;
return data;
}).promise();
return { statusCode: 200, body: JSON.stringify({msg: "SUCCESS"});
}catch(e){
return { statusCode: 500, body: JSON.stringify({...e, msg: "ERROR"});
}
}
I have 2 cases to test it.
CASE1
I request to update the data that exists in MY_TABLE
In this case, It works perfectly.
CASE2
I request to update the data that doesn't exist in MY_TABLE
it returns
{
"message": "Internal server error"
}
I don't expect a normal situation, but I believed it would be thrown to catch()
and I expect to see { statusCode: 500, body: JSON.stringify({...e, msg: "ERROR"});
.
so, how can I see that when I update to not exist data.
Upvotes: 0
Views: 861
Reputation: 13781
DynamoDB should fail the request for the non-existent item because the ConditionExpression
you specified will not be fulfilled. As explained in the DynamoDB documentation the error you'll get in this case will have the HTTP code 400, and its body (in JSON format) will contain the error type, "ConditionalCheckFailedException". You definitely don't expect to get an HTTP code 500 ("Internal Error") in this case - that can only happen in rare cases where the DynamoDB experiences unexpected problems, not normal requests.
Could it be that this error message is coming not from DynamoDB, but from some other layer in your setup? Can you try to send this same request directly to DynamoDB and see what it responds with?
Upvotes: 1