|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +{{>partial_header}} |
| 4 | +from typing import Any, Optional |
| 5 | +from typing_extensions import Self |
| 6 | + |
| 7 | +class OpenApiException(Exception): |
| 8 | + """The base exception class for all OpenAPIExceptions""" |
| 9 | + |
| 10 | + |
| 11 | +class ApiTypeError(OpenApiException, TypeError): |
| 12 | + def __init__(self, msg, path_to_item=None, valid_classes=None, |
| 13 | + key_type=None) -> None: |
| 14 | + """ Raises an exception for TypeErrors |
| 15 | + |
| 16 | + Args: |
| 17 | + msg (str): the exception message |
| 18 | + |
| 19 | + Keyword Args: |
| 20 | + path_to_item (list): a list of keys an indices to get to the |
| 21 | + current_item |
| 22 | + None if unset |
| 23 | + valid_classes (tuple): the primitive classes that current item |
| 24 | + should be an instance of |
| 25 | + None if unset |
| 26 | + key_type (bool): False if our value is a value in a dict |
| 27 | + True if it is a key in a dict |
| 28 | + False if our item is an item in a list |
| 29 | + None if unset |
| 30 | + """ |
| 31 | + self.path_to_item = path_to_item |
| 32 | + self.valid_classes = valid_classes |
| 33 | + self.key_type = key_type |
| 34 | + full_msg = msg |
| 35 | + if path_to_item: |
| 36 | + full_msg = "{0} at {1}".format(msg, render_path(path_to_item)) |
| 37 | + super(ApiTypeError, self).__init__(full_msg) |
| 38 | + |
| 39 | + |
| 40 | +class ApiValueError(OpenApiException, ValueError): |
| 41 | + def __init__(self, msg, path_to_item=None) -> None: |
| 42 | + """ |
| 43 | + Args: |
| 44 | + msg (str): the exception message |
| 45 | + |
| 46 | + Keyword Args: |
| 47 | + path_to_item (list) the path to the exception in the |
| 48 | + received_data dict. None if unset |
| 49 | + """ |
| 50 | + |
| 51 | + self.path_to_item = path_to_item |
| 52 | + full_msg = msg |
| 53 | + if path_to_item: |
| 54 | + full_msg = "{0} at {1}".format(msg, render_path(path_to_item)) |
| 55 | + super(ApiValueError, self).__init__(full_msg) |
| 56 | + |
| 57 | + |
| 58 | +class ApiAttributeError(OpenApiException, AttributeError): |
| 59 | + def __init__(self, msg, path_to_item=None) -> None: |
| 60 | + """ |
| 61 | + Raised when an attribute reference or assignment fails. |
| 62 | + |
| 63 | + Args: |
| 64 | + msg (str): the exception message |
| 65 | + |
| 66 | + Keyword Args: |
| 67 | + path_to_item (None/list) the path to the exception in the |
| 68 | + received_data dict |
| 69 | + """ |
| 70 | + self.path_to_item = path_to_item |
| 71 | + full_msg = msg |
| 72 | + if path_to_item: |
| 73 | + full_msg = "{0} at {1}".format(msg, render_path(path_to_item)) |
| 74 | + super(ApiAttributeError, self).__init__(full_msg) |
| 75 | + |
| 76 | + |
| 77 | +class ApiKeyError(OpenApiException, KeyError): |
| 78 | + def __init__(self, msg, path_to_item=None) -> None: |
| 79 | + """ |
| 80 | + Args: |
| 81 | + msg (str): the exception message |
| 82 | + |
| 83 | + Keyword Args: |
| 84 | + path_to_item (None/list) the path to the exception in the |
| 85 | + received_data dict |
| 86 | + """ |
| 87 | + self.path_to_item = path_to_item |
| 88 | + full_msg = msg |
| 89 | + if path_to_item: |
| 90 | + full_msg = "{0} at {1}".format(msg, render_path(path_to_item)) |
| 91 | + super(ApiKeyError, self).__init__(full_msg) |
| 92 | + |
| 93 | + |
| 94 | +class ApiException(OpenApiException): |
| 95 | + |
| 96 | + def __init__( |
| 97 | + self, |
| 98 | + status=None, |
| 99 | + reason=None, |
| 100 | + http_resp=None, |
| 101 | + *, |
| 102 | + body: Optional[str] = None, |
| 103 | + data: Optional[Any] = None, |
| 104 | + ) -> None: |
| 105 | + self.status = status |
| 106 | + self.reason = reason |
| 107 | + self.body = body |
| 108 | + self.data = data |
| 109 | + self.headers = None |
| 110 | + |
| 111 | + if http_resp: |
| 112 | + if self.status is None: |
| 113 | + self.status = http_resp.status |
| 114 | + if self.reason is None: |
| 115 | + self.reason = http_resp.reason |
| 116 | + if self.body is None: |
| 117 | + try: |
| 118 | + self.body = http_resp.data.decode('utf-8') |
| 119 | + except Exception: # noqa: S110 |
| 120 | + pass |
| 121 | + self.headers = http_resp.getheaders() |
| 122 | + |
| 123 | + @classmethod |
| 124 | + def from_response( |
| 125 | + cls, |
| 126 | + *, |
| 127 | + http_resp, |
| 128 | + body: Optional[str], |
| 129 | + data: Optional[Any], |
| 130 | + ) -> Self: |
| 131 | + if http_resp.status == 400: |
| 132 | + raise BadRequestException(http_resp=http_resp, body=body, data=data) |
| 133 | + |
| 134 | + if http_resp.status == 401: |
| 135 | + raise UnauthorizedException(http_resp=http_resp, body=body, data=data) |
| 136 | + |
| 137 | + if http_resp.status == 403: |
| 138 | + raise ForbiddenException(http_resp=http_resp, body=body, data=data) |
| 139 | + |
| 140 | + if http_resp.status == 404: |
| 141 | + raise NotFoundException(http_resp=http_resp, body=body, data=data) |
| 142 | + |
| 143 | + # Added new conditions for 409 and 422 |
| 144 | + if http_resp.status == 409: |
| 145 | + raise ConflictException(http_resp=http_resp, body=body, data=data) |
| 146 | + |
| 147 | + if http_resp.status == 422: |
| 148 | + raise UnprocessableEntityException(http_resp=http_resp, body=body, data=data) |
| 149 | + |
| 150 | + if 500 <= http_resp.status <= 599: |
| 151 | + raise ServiceException(http_resp=http_resp, body=body, data=data) |
| 152 | + raise ApiException(http_resp=http_resp, body=body, data=data) |
| 153 | + |
| 154 | + def __str__(self): |
| 155 | + """Custom error messages for exception""" |
| 156 | + error_message = "({0})\n"\ |
| 157 | + "Reason: {1}\n".format(self.status, self.reason) |
| 158 | + if self.headers: |
| 159 | + error_message += "HTTP response headers: {0}\n".format( |
| 160 | + self.headers) |
| 161 | + |
| 162 | + if self.data or self.body: |
| 163 | + error_message += "HTTP response body: {0}\n".format(self.data or self.body) |
| 164 | + |
| 165 | + return error_message |
| 166 | + |
| 167 | + |
| 168 | +class BadRequestException(ApiException): |
| 169 | + pass |
| 170 | + |
| 171 | + |
| 172 | +class NotFoundException(ApiException): |
| 173 | + pass |
| 174 | + |
| 175 | + |
| 176 | +class UnauthorizedException(ApiException): |
| 177 | + pass |
| 178 | + |
| 179 | + |
| 180 | +class ForbiddenException(ApiException): |
| 181 | + pass |
| 182 | + |
| 183 | + |
| 184 | +class ServiceException(ApiException): |
| 185 | + pass |
| 186 | + |
| 187 | + |
| 188 | +class ConflictException(ApiException): |
| 189 | + """Exception for HTTP 409 Conflict.""" |
| 190 | + pass |
| 191 | + |
| 192 | + |
| 193 | +class UnprocessableEntityException(ApiException): |
| 194 | + """Exception for HTTP 422 Unprocessable Entity.""" |
| 195 | + pass |
| 196 | + |
| 197 | + |
| 198 | +def render_path(path_to_item): |
| 199 | + """Returns a string representation of a path""" |
| 200 | + result = "" |
| 201 | + for pth in path_to_item: |
| 202 | + if isinstance(pth, int): |
| 203 | + result += "[{0}]".format(pth) |
| 204 | + else: |
| 205 | + result += "['{0}']".format(pth) |
| 206 | + return result |
0 commit comments