How to handle errors with boto3?

后端 未结 10 877
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 04:02

I am trying to figure how to do proper error handling with boto3.

I am trying to create an IAM user:

def create_user(username, iam_conn):
    try:
           


        
10条回答
  •  天命终不由人
    2020-12-02 04:35

    Following @armod's update about exceptions being added right on client objects. I'll show how you can see all exceptions defined for your client class.

    Exceptions are generated dynamically when you create your client with session.create_client() or boto3.client(). Internally it calls method botocore.errorfactory.ClientExceptionsFactory._create_client_exceptions() and fills client.exceptions field with constructed exception classes.

    All class names are available in client.exceptions._code_to_exception dictionary, so you can list all types with following snippet:

    client = boto3.client('s3')
    
    for ex_code in client.exceptions._code_to_exception:
        print(ex_code)
    

    Hope it helps.

提交回复
热议问题