gpt4 book ai didi

boto3 - 如何从 boto 导入特定错误?

转载 作者:行者123 更新时间:2023-12-02 10:33:10 31 4
gpt4 key购买 nike

运行代码时遇到此异常(打印类以进行调试):

An error occurred (DBInstanceNotFound) when calling the DescribeDBInstances operation: DBInstance <instance name> not found.
<class 'botocore.errorfactory.DBInstanceNotFoundFault'>

我想像这样正确处理异常:

from botocore.exceptions import DBInstanceNotFoundFault
from botocore.errorfactory import DBInstanceNotFoundFault
try:
<fetch info about db instance>
except DBInstanceNotFoundFault as e:
<handle error>

不幸的是,DBInstanceNotFoundFault既不存在于botocore.exceptions中,也不存在于botocore.errorfactory中。我克隆了boto3以及botocoregrep'd DBInstanceNotFoundFault,但就是找不到它。

如何导入?

最佳答案

对于 boto3 异常,您实际上并不导入错误 - 而是通过客户端访问它。看看这个:https://github.com/boto/boto3/issues/1195#issuecomment-317108970

import boto3
rds = boto3.client('rds')
try:
...
except rds.exceptions.DBInstanceNotFoundFault:
...

关于boto3 - 如何从 boto 导入特定错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48086787/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com