gpt4 book ai didi

python - 如何捕捉 `botocore.errorfactory.UserNotFoundException` ?

转载 作者:行者123 更新时间:2023-12-03 16:36:09 24 4
gpt4 key购买 nike

我正在使用 AWS Cognito制作 OAuth服务器。我现在正在创建异常处理程序,以防使用不存在,但 requests打算得到一个

ipdb> pk
'David'
ipdb> res = self.cognito_client.admin_get_user(
UserPoolId=settings.AWS_USER_POOL_ID,
Username=pk
)
*** botocore.errorfactory.UserNotFoundException: An error occurred (UserNotFoundException) when calling the AdminGetUser operation: User does not exist.
Traceback (most recent call last):
File "/Users/sarit/.pyenv/versions/futuready-titan/lib/python3.8/site-packages/botocore/client.py", line 316, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/Users/sarit/.pyenv/versions/futuready-titan/lib/python3.8/site-packages/botocore/client.py", line 626, in _make_api_call
raise error_class(parsed_response, operation_name)
boto3==1.12.15            # via -r el.in
botocore==1.15.15 # via boto3, s3transfer
django==3.0.3
python3.8.1

我查过 botocore source code UserNotFoundException
问题:
怎么才能具体 catchexception ?

最佳答案

有两种方法,如果异常暴露在客户端上,您可以直接捕获异常,或者从 botocore.exceptions 导入并使用它。
选项1:

try: 
res = self.cognito_client.admin_get_user(
UserPoolId=settings.AWS_USER_POOL_ID,
Username=pk
)
except self.cognito_client.exceptions.UserNotFoundException as e:
print(e)
选项 2:
from botocore.exceptions import UserNotFoundException

try:
res = self.cognito_client.admin_get_user(
UserPoolId=settings.AWS_USER_POOL_ID,
Username=pk
)
except UserNotFoundException as e:
print(e)
参见 botos 错误处理 documentation了解更多详细信息。

关于python - 如何捕捉 `botocore.errorfactory.UserNotFoundException` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60703127/

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