Setting the file. One moment.
Di Error Translation · AWS Observability · aws/agent-toolkit-for-aws · Skills Docs
ContentsBack to the top of the page 69
Creating Amazon Aurora Db Cluster With Instances
92
Routing Traffic With Route53 And CloudFront
Resilience Program Design
Debugging Lambda Timeouts
Next
Script Di Formatting
scripts/cloudwatch/ di_error_translation.py
Python · 155 lines · 6 KB
15 NoCredentialsError,
16 PartialCredentialsError,
17 ReadTimeoutError,
18 )
19
20
21 def _format_block (label: str , context: Optional[Mapping[ str , object ]]) -> str :
22 if not context:
23 return ""
24 lines = []
25 for key, value in context.items():
26 if value is None or value == "" :
27 continue
28 lines.append( f "- { key } : { value } " )
29 if not lines:
30 return ""
31 return f " \n{ label }\n " + " \n " .join(lines) + " \n "
32
33
34 def _format_attempted_block (context: Optional[Mapping[ str , object ]]) -> str :
35 return _format_block( "ATTEMPTED PARAMETERS:" , context)
36
37
38 def _format_numbered_section (label: str , items: Optional[Sequence[ str ]]) -> str :
39 if not items:
40 return ""
41 body = " \n " .join( f " { idx } . { item } " for idx, item in enumerate (items, 1 ))
42 return f " \n{ label }\n{ body }\n "
43
44
45 def _client_error_body (exc: ClientError) -> tuple[ str , str ]:
46 error = exc.response.get( "Error" , {}) if isinstance (exc.response, dict ) else {}
47 code = error.get( "Code" ) or "ClientError"
48 message = error.get( "Message" ) or str (exc)
49 return code, message
50
51
52 def render_client_error (
53 exc: ClientError,
54 * ,
55 action: str ,
56 attempted_label: str = "ATTEMPTED PARAMETERS:" ,
57 attempted: Optional[Mapping[ str , object ]] = None ,
58 possible_causes: Optional[Sequence[ str ]] = None ,
59 troubleshooting: Optional[Sequence[ str ]] = None ,
60 trailer: Optional[ str ] = None ,
61 ) -> str :
62 """Render a tool-tailored failure block for a botocore ``ClientError``.
63
64 Tools share the same skeleton — ``Failed to {action}``, an ``Error:`` line,
65 an attempted-values block, and ``POSSIBLE CAUSES`` / ``TROUBLESHOOTING``
66 numbered sections — but each tool tunes the labels and bullet content.
67 This helper takes those bullets as parameters so each call site can keep
68 its CLI-era wording without re-implementing the skeleton.
69
70 Use ``trailer`` for any tool-specific footer (e.g. the location
71 troubleshooting block emitted after a failed create).
72 """
73 code, message = _client_error_body(exc)
74 sections = [
75 f "Failed to { action }\n " ,
76 f " \n Error: { code } - { message }\n " ,
77 _format_block(attempted_label, attempted),
78 _format_numbered_section( "POSSIBLE CAUSES:" , possible_causes),
79 _format_numbered_section( "TROUBLESHOOTING:" , troubleshooting),
80 ]
81 body = "" .join(sections).rstrip()
82 if trailer:
83 return f " { body }\n\n{ trailer } "
84 return body
85
86
87 def translate_aws_error (
88 exc: BaseException ,
89 * ,
90 action: str ,
91 context: Optional[Mapping[ str , object ]] = None ,
92 ) -> str :
93 """Render a human-readable failure block for an AWS API exception.
94
95 Args:
96 exc: The exception raised by a boto3/botocore call.
97 action: A short verb phrase such as ``"create BREAKPOINT instrumentation"``.
98 context: Optional ordered mapping of attempted parameters.
99
100 Returns:
101 A multi-line string starting with ``Failed to {action}`` and including
102 an ``Error:`` line, an ``ATTEMPTED PARAMETERS:`` block when context
103 is provided, and standard ``POSSIBLE CAUSES``/``TROUBLESHOOTING``
104 sections tuned to the exception type.
105 """
106 attempted = _format_attempted_block(context)
107
108 if isinstance (exc, ClientError):
109 code, message = _client_error_body(exc)
110 return (
111 f "Failed to { action }\n\n "
112 f "Error: { code } - { message }\n "
113 f " { attempted } "
114 " \n POSSIBLE CAUSES: \n "
115 "1. Invalid input parameters (validation error) \n "
116 "2. Resource not found, already exists, or scoped to a different account \n "
117 "3. Insufficient IAM permissions \n "
118 "4. Service-side throttling or transient error \n "
119 " \n TROUBLESHOOTING: \n "
120 "1. Re-read the error message above for the specific failure cause \n "
121 "2. Verify service, environment, and instrumentation_type identifiers \n "
122 "3. Verify credentials map to an account/region with access \n "
123 )
124
125 if isinstance (exc, EndpointConnectionError):
126 return (
127 f "Failed to { action }\n\n "
128 f "Error: EndpointConnectionError - { exc }\n "
129 f " { attempted } "
130 " \n TROUBLESHOOTING: \n "
131 "1. Check network connectivity to the AWS endpoint \n "
132 "2. Verify AWS region resolution (AWS_REGION env var or profile) \n "
133 )
134
135 if isinstance (exc, (ReadTimeoutError, ConnectTimeoutError)):
136 return (
137 f "Failed to { action }\n\n "
138 f "Error: TimeoutError - { exc }\n "
139 f " { attempted } "
140 " \n TROUBLESHOOTING: \n "
141 "1. Retry the request — the AWS endpoint did not respond within the socket timeout \n "
142 "2. Check network connectivity \n "
143 )
144
145 if isinstance (exc, (NoCredentialsError, PartialCredentialsError)):
146 return (
147 f "Failed to { action }\n\n "
148 f "Error: { type (exc). __name__ } - { exc }\n "
149 f " { attempted } "
150 " \n TROUBLESHOOTING: \n "
151 "1. Verify AWS credentials: aws configure list \n "
152 "2. Set AWS_PROFILE or supply credentials via env vars \n "
153 )
154
155 return f "Failed to { action }\n\n Unexpected error: { exc }\n{ attempted } "