From 63afb33d8669ffc9180e017c4326cd25bf396fd3 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Tue, 27 Apr 2021 14:19:00 +0200 Subject: [PATCH] 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 --- .../rst/dev_guide/platforms/aws_guidelines.rst | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/docs/docsite/rst/dev_guide/platforms/aws_guidelines.rst b/docs/docsite/rst/dev_guide/platforms/aws_guidelines.rst index 27cafb722fa..ffc41b26d14 100644 --- a/docs/docsite/rst/dev_guide/platforms/aws_guidelines.rst +++ b/docs/docsite/rst/dev_guide/platforms/aws_guidelines.rst @@ -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