Use is_boto3_error_code in 'standard' example (#72313)

Use is_boto3_error_code in 'standard' example rather than e.response['Error']['Code'] (#72313)

Co-authored-by: Sloane Hertel <shertel@redhat.com>
This commit is contained in:
Mark Chappell 2021-04-27 14:19:00 +02:00 committed by GitHub
parent cf08c23b4f
commit 63afb33d86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -287,7 +287,8 @@ amounts of exception handling to existing modules, we recommend migrating the mo
Note that it should normally be acceptable to catch all normal exceptions here, however if you
expect anything other than botocore exceptions you should test everything works as expected.
If you need to perform an action based on the error boto3 returned, use the error code.
If you need to perform an action based on the error boto3 returned, use the error code and the
``is_boto3_error_code()`` helper.
.. code-block:: python
@ -295,12 +296,9 @@ If you need to perform an action based on the error boto3 returned, use the erro
name = module.params.get['name']
try:
result = connection.describe_frooble(FroobleName=name)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'FroobleNotFound':
workaround_failure() # This is an error that we can work around
else:
module.fail_json_aws(e, msg="Couldn't obtain frooble %s" % name)
except botocore.exceptions.BotoCoreError as e:
except is_boto3_error_code('FroobleNotFound'):
workaround_failure() # This is an error that we can work around
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Couldn't obtain frooble %s" % name)
using fail_json() and avoiding ansible_collections.amazon.aws.plugins.module_utils.core