Search code examples
pythonamazon-web-servicesamazon-s3boto3

correct way for catching aws boto3 errors


why to use botocore.exceptions.ClientError for catching instead of python inbuilt Exception, what is the recommended way and what are the advantages of using botocore exceptions I have the following codes

except botocore.exceptions.ClientError as error:
        logger.info(error)
except Exception as error:
        logger.info(str(error))

For the above code snippets the output through logger is same when object doesn't exists or wrong key is given

INFO:root:An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.


Solution

  • If you use botocore.exceptions.ClientError then you will get specifically identifies the error as an AWS client error and this error helps you to solve more errors that are related to AWS libraries in the future.

    Also you can use simple python Exception error handling. But I think you need to use botocore.exceptions.ClientError handler for dealing specific error resolutions in the future.