我正在使用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源代码 UserNotFoundException查过了
问题:
我如何才能特别地catch这个exception
发布于 2021-07-25 19:11:45
当botocore.error_factory创建异常时,就不可能直接导入它。您应该使用生成的类来处理异常,而不是直接导入。
为文档中的每个操作提供了可能的异常列表。例如,对于用户,可能的例外是:
CognitoIdentityProvider.Client.exceptions.ResourceNotFoundException
CognitoIdentityProvider.Client.exceptions.InvalidParameterException
CognitoIdentityProvider.Client.exceptions.TooManyRequestsException
CognitoIdentityProvider.Client.exceptions.NotAuthorizedException
CognitoIdentityProvider.Client.exceptions.UserNotFoundException
CognitoIdentityProvider.Client.exceptions.InternalErrorException有一个例子说明了如何获取客户端的示例列表以及如何处理它(当然,异常列表取决于操作):
import boto3
eks = boto3.client('eks')
print(dir(eks.exceptions))
# ['BadRequestException',
# 'ClientError',
# 'ClientException',
# 'InvalidParameterException',
# 'InvalidRequestException',
# 'NotFoundException',
# 'ResourceInUseException',
# 'ResourceLimitExceededException',
# 'ResourceNotFoundException',
# 'ServerException',
# 'ServiceUnavailableException',
# 'UnsupportedAvailabilityZoneException', ...]
try:
response = eks.list_nodegroups(clusterName='my-cluster')
except eks.exceptions.ResourceNotFoundException as e:
# do something with e
print("handled: " + str(e))
cognito_idp = boto3.client('cognito-idp')
print(dir(cognito_idp.exceptions))
# [ 'ClientError',
# 'ConcurrentModificationException',
# 'DeveloperUserAlreadyRegisteredException',
# 'ExternalServiceException',
# 'InternalErrorException',
# 'InvalidIdentityPoolConfigurationException',
# 'InvalidParameterException',
# 'LimitExceededException',
# 'NotAuthorizedException',
# 'ResourceConflictException',
# 'ResourceNotFoundException',
# 'TooManyRequestsException', ... ]
try:
response = cognito_idp.admin_get_user(
UserPoolId='pool_id',
Username='username'
)
except cognito_idp.exceptions.UserNotFoundException as e:
# do something with e
print("handled: " + str(e))此外,您可以捕获较少类型的botocore.exceptions.ClientError,而不是特定的:
import boto3
import botocore.exceptions
try:
response = cognito_idp.admin_get_user(
UserPoolId='pool_id',
Username='username'
)
except botocore.exceptions.ClientError as e:
# do something with e
print("handled: " + str(e))发布于 2020-06-22 21:29:47
有两种方法,如果异常在客户机上公开,或者从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错误处理文档。
发布于 2020-08-12 19:58:56
这当然不是理想的做法,但我可以用以下方法来理解:
from botocore.exceptions import ClientError
try:
func_that_interacts_with_cognito()
except ClientError:
# This happens when the user is not found.
print("It happened again ...")https://stackoverflow.com/questions/60703127
复制相似问题