我试图弄清楚如何使用boto3进行正确的错误处理。

我正在尝试创建一个IAM用户:

def create_user(username, iam_conn):
    try:
        user = iam_conn.create_user(UserName=username)
        return user
    except Exception as e:
        return e

当调用create_user成功时,我得到一个整洁的对象,其中包含API调用的http状态代码和新创建用户的数据。

例子:

{'ResponseMetadata': 
      {'HTTPStatusCode': 200, 
       'RequestId': 'omitted'
      },
 u'User': {u'Arn': 'arn:aws:iam::omitted:user/omitted',
           u'CreateDate': datetime.datetime(2015, 10, 11, 17, 13, 5, 882000, tzinfo=tzutc()),
           u'Path': '/',
           u'UserId': 'omitted',
           u'UserName': 'omitted'
          }
}

这很有效。但是当这个失败时(比如如果用户已经存在),我只得到一个botocore.exceptions.ClientError类型的对象,其中只有文本告诉我哪里出错了。

例子: ClientError('调用CreateUser操作时发生错误(EntityAlreadyExists):省略名称的用户已经存在。')

这(AFAIK)使得错误处理非常困难,因为我不能只是打开结果的http状态代码(409用户已经存在根据AWS API文档的IAM)。这让我觉得我一定是做错了什么。最优的方法是boto3永远不抛出异常,但juts总是返回一个反映API调用如何进行的对象。

有没有人能在这个问题上给我一些启发,或者给我指出正确的方向?


当前回答

只是更新了@jarmod指出的“资源上没有例外”问题(如果下面似乎适用,请随时更新您的答案)

我已经测试了下面的代码,它运行良好。它使用“资源”来做事情,但捕获客户端。例外-尽管它“看起来”有点错误…它测试得很好,在异常时使用调试器查看异常类时显示和匹配……

它可能不适用于所有资源和客户端,但适用于数据文件夹(又名s3桶)。

lab_session = boto3.Session() 
c = lab_session.client('s3') #this client is only for exception catching

try:
    b = s3.Bucket(bucket)
    b.delete()
except c.exceptions.NoSuchBucket as e:
    #ignoring no such bucket exceptions
    logger.debug("Failed deleting bucket. Continuing. {}".format(e))
except Exception as e:
    #logging all the others as warning
    logger.warning("Failed deleting bucket. Continuing. {}".format(e))

希望这对你有所帮助……

其他回答

当它不能处理问题时,你需要做些什么。现在您返回的是实际的异常。 例如,如果用户已经存在而你想使用它作为get_or_create函数,也许你可以通过返回现有的用户对象来处理这个问题。

try:
    user = iam_conn.create_user(UserName=username)
    return user
except botocore.exceptions.ClientError as e:

    #this exception could actually be other things other than exists, so you want to evaluate it further in your real code.
    if e.message.startswith(
        'enough of the exception message to identify it as the one you want')

        print('that user already exists.')
        user = iam_conn.get_user(UserName=username)
        return user

    elif e.message.some_other_condition:

         #something else
    else:
         #unhandled ClientError
         raise(e)
except SomeOtherExceptionTypeYouCareAbout as e:
    #handle it

# any unhandled exception will raise here at this point.
# if you want a general handler

except Exception as e:
    #handle it.

也就是说,这可能是你的应用程序的问题,在这种情况下,你想要在调用create user函数的代码周围放置异常处理程序并让调用函数决定如何处理它,例如,通过要求用户输入另一个用户名,或任何对你的应用程序有意义的东西。

跟随@armod关于在客户端对象上添加异常的更新。我将展示如何查看为客户端类定义的所有异常。

异常是在使用session.create_client()或boto3.client()创建客户端时动态生成的。它在内部调用方法botocore.errorfactory.ClientExceptionsFactory._create_client_exceptions()并填充客户端。带有构造异常类的异常字段。

所有的类名都在client.exceptions中可用。_code_to_exception字典,所以你可以用下面的代码段列出所有类型:

client = boto3.client('s3')

for ex_code in client.exceptions._code_to_exception:
    print(ex_code)

希望能有所帮助。

只需要一个导入。 不需要if语句。 按预期使用客户端内置异常。

Ex:

from boto3 import client

cli = client('iam')
try:
    cli.create_user(
        UserName = 'Brian'
    )
except cli.exceptions.EntityAlreadyExistsException:
    pass

CloudWatch示例:

cli = client('logs')
try:
    cli.create_log_group(
        logGroupName = 'MyLogGroup'
    )
except cli.exceptions.ResourceAlreadyExistsException:
    pass

只是更新了@jarmod指出的“资源上没有例外”问题(如果下面似乎适用,请随时更新您的答案)

我已经测试了下面的代码,它运行良好。它使用“资源”来做事情,但捕获客户端。例外-尽管它“看起来”有点错误…它测试得很好,在异常时使用调试器查看异常类时显示和匹配……

它可能不适用于所有资源和客户端,但适用于数据文件夹(又名s3桶)。

lab_session = boto3.Session() 
c = lab_session.client('s3') #this client is only for exception catching

try:
    b = s3.Bucket(bucket)
    b.delete()
except c.exceptions.NoSuchBucket as e:
    #ignoring no such bucket exceptions
    logger.debug("Failed deleting bucket. Continuing. {}".format(e))
except Exception as e:
    #logging all the others as warning
    logger.warning("Failed deleting bucket. Continuing. {}".format(e))

希望这对你有所帮助……

或者是类名的比较。

except ClientError as e:
    if 'EntityAlreadyExistsException' == e.__class__.__name__:
        # handle specific error

因为它们是动态创建的,所以永远不能导入类并使用真正的Python捕获它。