Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/botocore/errorfactory.py: 38%
32 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
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.
13from botocore.exceptions import ClientError
14from botocore.utils import get_service_module_name
17class BaseClientExceptions:
18 ClientError = ClientError
20 def __init__(self, code_to_exception):
21 """Base class for exceptions object on a client
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 self._code_to_exception = code_to_exception
30 def from_code(self, error_code):
31 """Retrieves the error class based on the error code
33 This is helpful for identifying the exception class needing to be
34 caught based on the ClientError.parsed_reponse['Error']['Code'] value
36 :type error_code: string
37 :param error_code: The error code associated to a ClientError exception
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 return self._code_to_exception.get(error_code, self.ClientError)
46 def __getattr__(self, name):
47 exception_cls_names = [
48 exception_cls.__name__
49 for exception_cls in self._code_to_exception.values()
50 ]
51 raise AttributeError(
52 fr"{self} object has no attribute {name}. "
53 fr"Valid exceptions are: {', '.join(exception_cls_names)}"
54 )
57class ClientExceptionsFactory:
58 def __init__(self):
59 self._client_exceptions_cache = {}
61 def create_client_exceptions(self, service_model):
62 """Creates a ClientExceptions object for the particular service client
64 :type service_model: botocore.model.ServiceModel
65 :param service_model: The service model for the client
67 :rtype: object that subclasses from BaseClientExceptions
68 :returns: The exceptions object of a client that can be used
69 to grab the various different modeled exceptions.
70 """
71 service_name = service_model.service_name
72 if service_name not in self._client_exceptions_cache:
73 client_exceptions = self._create_client_exceptions(service_model)
74 self._client_exceptions_cache[service_name] = client_exceptions
75 return self._client_exceptions_cache[service_name]
77 def _create_client_exceptions(self, service_model):
78 cls_props = {}
79 code_to_exception = {}
80 for error_shape in service_model.error_shapes:
81 exception_name = str(error_shape.name)
82 exception_cls = type(exception_name, (ClientError,), {})
83 cls_props[exception_name] = exception_cls
84 code = str(error_shape.error_code)
85 code_to_exception[code] = exception_cls
86 cls_name = str(get_service_module_name(service_model) + 'Exceptions')
87 client_exceptions_cls = type(
88 cls_name, (BaseClientExceptions,), cls_props
89 )
90 return client_exceptions_cls(code_to_exception)