Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/errorhandler.py
1566 views
1
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
import logging
14
15
LOG = logging.getLogger(__name__)
16
17
18
class BaseOperationError(Exception):
19
MSG_TEMPLATE = (
20
"A {error_type} error ({error_code}) occurred "
21
"when calling the {operation_name} operation: "
22
"{error_message}"
23
)
24
25
def __init__(
26
self,
27
error_code,
28
error_message,
29
error_type,
30
operation_name,
31
http_status_code,
32
):
33
msg = self.MSG_TEMPLATE.format(
34
error_code=error_code,
35
error_message=error_message,
36
error_type=error_type,
37
operation_name=operation_name,
38
)
39
super().__init__(msg)
40
self.error_code = error_code
41
self.error_message = error_message
42
self.error_type = error_type
43
self.operation_name = operation_name
44
self.http_status_code = http_status_code
45
46
47
class ClientError(BaseOperationError):
48
pass
49
50
51
class ServerError(BaseOperationError):
52
pass
53
54
55
class ErrorHandler:
56
"""
57
This class is responsible for handling any HTTP errors that occur
58
when a service operation is called. It is registered for the
59
``after-call`` event and will have the opportunity to inspect
60
all operation calls. If the HTTP response contains an error
61
``status_code`` an appropriate error message will be printed and
62
the handler will short-circuit all further processing by exiting
63
with an appropriate error code.
64
"""
65
66
def __call__(self, http_response, parsed, model, **kwargs):
67
LOG.debug('HTTP Response Code: %d', http_response.status_code)
68
error_type = None
69
error_class = None
70
if http_response.status_code >= 500:
71
error_type = 'server'
72
error_class = ServerError
73
elif (
74
http_response.status_code >= 400
75
or http_response.status_code == 301
76
):
77
error_type = 'client'
78
error_class = ClientError
79
if error_class is not None:
80
code, message = self._get_error_code_and_message(parsed)
81
raise error_class(
82
error_code=code,
83
error_message=message,
84
error_type=error_type,
85
operation_name=model.name,
86
http_status_code=http_response.status_code,
87
)
88
89
def _get_error_code_and_message(self, response):
90
code = 'Unknown'
91
message = 'Unknown'
92
if 'Error' in response:
93
error = response['Error']
94
return error.get('Code', code), error.get('Message', message)
95
return (code, message)
96
97