How to catch `botocore.errorfactory.UserNotFoundException`?

When an exception is created by botocore.error_factory then it is not possible to directly import it. Instead of direct import, you should use generated classes for exceptions. A list of possible exceptions is provided for each operation in the documentation. For example, for CognitoIdentityProvider.Client.admin_get_user, possible exceptions are: CognitoIdentityProvider.Client.exceptions.ResourceNotFoundException CognitoIdentityProvider.Client.exceptions.InvalidParameterException CognitoIdentityProvider.Client.exceptions.TooManyRequestsException CognitoIdentityProvider.Client.exceptions.NotAuthorizedException CognitoIdentityProvider.Client.exceptions.UserNotFoundException CognitoIdentityProvider.Client.exceptions.InternalErrorException There is … Read more

How to view Boto3 HTTPS request string

You could also enable debug logging in boto3. That will log all requests and responses as well as lots of other things. Its a bit obscure to enable it: import boto3 boto3.set_stream_logger(name=”botocore”) The reason you have to specify botocore as the name to log is that all of the actual requests and responses happen at … Read more

Mocking boto3 S3 client method Python

Botocore has a client stubber you can use for just this purpose: docs. Here’s an example of putting an error in: import boto3 from botocore.stub import Stubber client = boto3.client(‘s3’) stubber = Stubber(client) stubber.add_client_error(‘upload_part_copy’) stubber.activate() # Will raise a ClientError client.upload_part_copy() Here’s an example of putting a normal response in. Additionally, the stubber can now … Read more

How to capture botocore’s NoSuchKey exception?

from botocore.exceptions import ClientError try: response = self.client.get_object(Bucket=bucket, Key=key) return json.loads(response[“Body”].read()) except ClientError as ex: if ex.response[‘Error’][‘Code’] == ‘NoSuchKey’: logger.info(‘No object found – returning empty’) return dict() else: raise

tech