summaryrefslogtreecommitdiff
path: root/lib/ansible/module_utils/aws/core.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ansible/module_utils/aws/core.py')
-rw-r--r--lib/ansible/module_utils/aws/core.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/ansible/module_utils/aws/core.py b/lib/ansible/module_utils/aws/core.py
index 99a87dd440..466e72b031 100644
--- a/lib/ansible/module_utils/aws/core.py
+++ b/lib/ansible/module_utils/aws/core.py
@@ -254,3 +254,25 @@ class _RetryingBotoClientWrapper(object):
return wrapped
else:
return unwrapped
+
+
+def is_boto3_error_code(code, e=None):
+ """Check if the botocore exception is raised by a specific error code.
+
+ Returns ClientError if the error code matches, a dummy exception if it does not have an error code or does not match
+
+ Example:
+ try:
+ ec2.describe_instances(InstanceIds=['potato'])
+ except is_boto3_error_code('InvalidInstanceID.Malformed'):
+ # handle the error for that code case
+ except botocore.exceptions.ClientError as e:
+ # handle the generic error case for all other codes
+ """
+ from botocore.exceptions import ClientError
+ if e is None:
+ import sys
+ dummy, e, dummy = sys.exc_info()
+ if isinstance(e, ClientError) and e.response['Error']['Code'] == code:
+ return ClientError
+ return type('NeverEverRaisedException', (Exception,), {})