Debian Codegraph @

ansible_collections/amazon/aws/plugins/module_utils/exceptions.py

raw
1 # -*- coding: utf-8 -*-
2
3 # (c) 2022 Red Hat Inc.
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from ansible.module_utils.common.text.converters import to_native
7
8
9 class AnsibleAWSError(Exception):
10 def __str__(self):
11 if self.exception and self.message:
12 return f"{self.message}: {to_native(self.exception)}"
13
14 return super().__str__()
15
16 def __init__(self, message=None, exception=None, **kwargs):
17 if not message and not exception:
18 super().__init__()
19 elif not message:
20 super().__init__(exception)
21 else:
22 super().__init__(message)
23
24 self.exception = exception
25 self.message = message
26
27 # In places where passing more information to module.fail_json would be helpful
28 # store the extra info. Other plugin types have to raise the correct exception
29 # such as AnsibleLookupError, so can't easily consume this.
30 self.kwargs = kwargs or {}
31
32
33 def is_ansible_aws_error_code(code, e=None):
34 """Check if the AnsibleAWSError exception is raised by a botocore exception with specific error code.
35
36 Returns AnsibleAWSError if the error code matches, a dummy exception if it does not have an error code or does not match
37
38 Example:
39 try:
40 describe_instances(connection, InstanceIds=['potato'])
41 except is_ansible_aws_error_code('InvalidInstanceID.Malformed'):
42 # handle the error for that code case
43 except AnsibleAWSError as e:
44 # handle the generic error case for all other codes
45 """
46 from botocore.exceptions import ClientError
47
48 if e is None:
49 import sys
50
51 e = sys.exc_info()[1]
52 if not isinstance(code, (list, tuple, set)):
53 code = [code]
54 if (
55 isinstance(e, AnsibleAWSError)
56 and e.exception
57 and isinstance(e.exception, ClientError)
58 and e.exception.response["Error"]["Code"] in code
59 ):
60 return AnsibleAWSError
61 return type("NeverEverRaisedException", (Exception,), {})
62
63
64 def is_ansible_aws_error_message(msg, e=None):
65 """Check if the AnsibleAWSError exception raised by a botocore exception contains a specific error message.
66
67 Returns AnsibleAWSError if the error code matches, a dummy exception if it does not have an error code or does not match
68
69 Example:
70 try:
71 describe_vpc_classic_link(connection, VpcIds=[vpc_id])
72 except is_ansible_aws_error_message('The functionality you requested is not available in this region.'):
73 # handle the error for that error message
74 except AnsibleAWSError as e:
75 # handle the generic error case for all other codes
76 """
77 from botocore.exceptions import ClientError
78
79 if e is None:
80 import sys
81
82 e = sys.exc_info()[1]
83 if (
84 isinstance(e, AnsibleAWSError)
85 and e.exception
86 and isinstance(e.exception, ClientError)
87 and msg in e.exception.response["Error"]["Message"]
88 ):
89 return AnsibleAWSError
90 return type("NeverEverRaisedException", (Exception,), {})
91
92
93 class AnsibleBotocoreError(AnsibleAWSError):
94 pass