1
# Copyright 2016 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 11
from botocore.exceptions import ClientError
14 11
from botocore.utils import get_service_module_name
15

16

17 11
class BaseClientExceptions(object):
18 11
    ClientError = ClientError
19

20 11
    def __init__(self, code_to_exception):
21
        """Base class for exceptions object on a client
22

23
        :type code_to_exception: dict
24
        :param code_to_exception: Mapping of error codes (strings) to exception
25
            class that should be raised when encountering a particular
26
            error code.
27
        """
28 11
        self._code_to_exception = code_to_exception
29

30 11
    def from_code(self, error_code):
31
        """Retrieves the error class based on the error code
32

33
        This is helpful for identifying the exception class needing to be
34
        caught based on the ClientError.parsed_reponse['Error']['Code'] value
35

36
        :type error_code: string
37
        :param error_code: The error code associated to a ClientError exception
38

39
        :rtype: ClientError or a subclass of ClientError
40
        :returns: The appropriate modeled exception class for that error
41
            code. If the error code does not match any of the known
42
            modeled exceptions then return a generic ClientError.
43
        """
44 11
        return self._code_to_exception.get(error_code, self.ClientError)
45

46 11
    def __getattr__(self, name):
47 11
        exception_cls_names = [
48
            exception_cls.__name__ for exception_cls
49
            in self._code_to_exception.values()
50
        ]
51 11
        raise AttributeError(
52
            '%r object has no attribute %r. Valid exceptions are: %s' % (
53
                self, name, ', '.join(exception_cls_names)))
54

55

56 11
class ClientExceptionsFactory(object):
57 11
    def __init__(self):
58 11
        self._client_exceptions_cache = {}
59

60 11
    def create_client_exceptions(self, service_model):
61
        """Creates a ClientExceptions object for the particular service client
62

63
        :type service_model: botocore.model.ServiceModel
64
        :param service_model: The service model for the client
65

66
        :rtype: object that subclasses from BaseClientExceptions
67
        :returns: The exceptions object of a client that can be used
68
            to grab the various different modeled exceptions.
69
        """
70 11
        service_name = service_model.service_name
71 11
        if service_name not in self._client_exceptions_cache:
72 11
            client_exceptions = self._create_client_exceptions(service_model)
73 11
            self._client_exceptions_cache[service_name] = client_exceptions
74 11
        return self._client_exceptions_cache[service_name]
75

76 11
    def _create_client_exceptions(self, service_model):
77 11
        cls_props = {}
78 11
        code_to_exception = {}
79 11
        for error_shape in service_model.error_shapes:
80 11
            exception_name = str(error_shape.name)
81 11
            exception_cls = type(exception_name, (ClientError,), {})
82 11
            cls_props[exception_name] = exception_cls
83 11
            code = str(error_shape.error_code)
84 11
            code_to_exception[code] = exception_cls
85 11
        cls_name = str(get_service_module_name(service_model) + 'Exceptions')
86 11
        client_exceptions_cls = type(
87
            cls_name, (BaseClientExceptions,), cls_props)
88 11
        return client_exceptions_cls(code_to_exception)

Read our documentation on viewing source code .

Loading