0

I'm getting a Boto3 InvalidParameterException while running the lambda function. I'm trying to find out a way to handle this exception.

I came across the below solution:

from boto.exception import BotoServerError

class InvalidParameterException(BotoServerError):
    pass

I'm using python3 and understood that boto is deprecated now and is replaced by boto3. But i could not find an equivalent solution in boto3.

Can anyone help me out with this ?

Bharath Bharath
  • 59
  • 1
  • 10

1 Answers1

0

As boto is deprecated all the modeled exceptions are available on the client. You can look it up same in the API docs as well , basically the code for the boto3 is straight away generated from the APIs. Earlier approach with boto was hard coded stuff and writing code for the same.

As you can see here

For example

import boto3
from botocore.exceptions import ClientError


def get_secret():
    secret_name = "MySecretName"
    region_name = "us-west-2"

    session = boto3.session.Session()
    client = session.client(
        service_name='secretsmanager',
        region_name=region_name,
    )

    try:
        get_secret_value_response = client.get_secret_value(
            SecretId=secret_name
        )
    except ClientError as e:
        if e.response['Error']['Code'] == 'ResourceNotFoundException':
            print("The requested secret " + secret_name + " was not found")
        elif e.response['Error']['Code'] == 'InvalidRequestException':
            print("The request was invalid due to:", e)
        elif e.response['Error']['Code'] == 'InvalidParameterException':
            print("The request had invalid params:", e)
        elif e.response['Error']['Code'] == 'DecryptionFailure':
            print("The requested secret can't be decrypted using the provided KMS key:", e)
        elif e.response['Error']['Code'] == 'InternalServiceError':
            print("An error occurred on service side:", e)

AWS Secrets Manager Example From the docss

How to handle errors with boto3

samtoddler
  • 8,463
  • 2
  • 26
  • 21
  • Thank you! This works for me. The botocore library generates several exceptions from a base class. So, it's better to Catch the baseclass and then handle the exception as we need – Bharath Bharath Mar 31 '21 at 09:41