Questions

How to handle DynamoDB exceptions?

0

I am trying to call scan() on DynamoDB table but it says ResourceNotFoundException.

How I can handle this exception?

Error:

An error occurred (ResourceNotFoundException) when calling the Scan operation: Requested resource not found

Code with which I tried to handle the exception.

import boto3
from boto3.dynamodb.conditions import Attr

dynamodb_resource = boto3.resource('dynamodb')

table = dynamodb_resource.Table("reviewers")
try:
    response = table.scan(
        FilterExpression=Attr('user').contains("xyzname")
    )
    count = response['Count']
except dynamodb_resource.exceptions.ResourceNotFoundException:
    count = 0

return count

But now I am getting

New Error:

AttributeError: ‘dynamodb.ServiceResource’ object has no attribute ‘exceptions’.

How to resolve this?



asked 10 months ago
Reputation: 1





1 Answer

0

Actually problem is all service exceptions are available inside .exceptions of the client.

Use client to handle exceptions and resolve the error.

Resolved Code:

import boto3
from boto3.dynamodb.conditions import Attr

dynamodb_resource = boto3.resource('dynamodb')
dynamodb_client = boto3.client('dynamodb')

table = dynamodb_resource.Table("reviewers")
try:
    response = table.scan(
        FilterExpression=Attr('user').contains("xyzname")
    )
    count = response['Count']
except dynamodb_client.exceptions.ResourceNotFoundException:
    count = 0

return count

Hope this helps someone !!!.

answered 10 months ago
Reputation: 1





Your Answer

Nothing to preview

Post Answer



Asked:  10 months ago
Viewed:  915 times
Active:  10 months ago