mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 11:21:25 -07:00
Add AWS boto3 error code exception function is_boto3_error_code (#41202)
* Add aws/core.py function to check for specific AWS error codes * Use sys.exc_info to get exception object if it isn't passed in * Allow catching exceptions with is_boto3_error_code * Replace from_code with is_boto3_error_code * Return a type that will never be raised to support stricter type comparisons in Python 3+ * Use is_boto3_error_code in aws_eks_cluster * Add duplicate-except to ignores when using is_boto3_error_code * Add is_boto3_error_code to module development guideline docs
This commit is contained in:
parent
269f404121
commit
40d2df0ef3
12 changed files with 84 additions and 41 deletions
|
@ -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,), {})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue