File: exceptions.py

package info (click to toggle)
python-openstacksdk 4.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,352 kB
  • sloc: python: 122,960; sh: 153; makefile: 23
file content (278 lines) | stat: -rw-r--r-- 8,788 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Copyright 2010 Jacob Kaplan-Moss
# Copyright 2011 Nebula, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
"""
Exception definitions.
"""

import json
import re
import typing as ty

import requests
from requests import exceptions as _rex

if ty.TYPE_CHECKING:
    from openstack import resource


class SDKException(Exception):
    """The base exception class for all exceptions this library raises."""

    def __init__(
        self, message: ty.Optional[str] = None, extra_data: ty.Any = None
    ):
        self.message = self.__class__.__name__ if message is None else message
        self.extra_data = extra_data
        super().__init__(self.message)


class EndpointNotFound(SDKException):
    """A mismatch occurred between what the client and server expect."""

    def __init__(self, message: ty.Optional[str] = None):
        super().__init__(message)


class InvalidResponse(SDKException):
    """The response from the server is not valid for this request."""

    def __init__(self, message: ty.Optional[str] = None):
        super().__init__(message)


class InvalidRequest(SDKException):
    """The request to the server is not valid."""

    def __init__(self, message: ty.Optional[str] = None):
        super().__init__(message)


class HttpException(SDKException, _rex.HTTPError):
    """The base exception for all HTTP error responses."""

    source: str
    status_code: ty.Optional[int]

    def __init__(
        self,
        message: ty.Optional[str] = 'Error',
        response: ty.Optional[requests.Response] = None,
        http_status: ty.Optional[int] = None,
        details: ty.Optional[str] = None,
        request_id: ty.Optional[str] = None,
    ):
        # TODO(shade) Remove http_status parameter and the ability for response
        # to be None once we're not mocking Session everywhere.
        if not message:
            if response is not None:
                message = f"{self.__class__.__name__}: {response.status_code}"
            else:
                message = f"{self.__class__.__name__}: Unknown error"

        # Call directly rather than via super to control parameters
        SDKException.__init__(self, message=message)
        _rex.HTTPError.__init__(self, message, response=response)

        if response is not None:
            self.request_id = response.headers.get('x-openstack-request-id')
            self.status_code = response.status_code
        else:
            self.request_id = request_id
            self.status_code = http_status
        self.details = details
        self.url = self.request and self.request.url or None
        self.method = self.request and self.request.method or None
        self.source = "Server"
        if self.status_code is not None and (400 <= self.status_code < 500):
            self.source = "Client"

    def __str__(self) -> str:
        # 'Error' is the default value for self.message. If self.message isn't
        # 'Error', then someone has set a more informative error message
        # and we should use it. If it is 'Error', then we should construct a
        # better message from the information we do have.
        if not self.url or self.message == 'Error':
            return self.message
        if self.url:
            remote_error = f"{self.source} Error for url: {self.url}"
            if self.details:
                remote_error += ', '
        if self.details:
            remote_error += str(self.details)

        return f"{super().__str__()}: {remote_error}"


class BadRequestException(HttpException):
    """HTTP 400 Bad Request."""


class NotFoundException(HttpException):
    """HTTP 404 Not Found."""


class ForbiddenException(HttpException):
    """HTTP 403 Forbidden Request."""


class ConflictException(HttpException):
    """HTTP 409 Conflict."""


class PreconditionFailedException(HttpException):
    """HTTP 412 Precondition Failed."""


class MethodNotSupported(SDKException):
    """The resource does not support this operation type."""

    def __init__(
        self,
        resource: ty.Union['resource.Resource', type['resource.Resource']],
        method: str,
    ):
        # This needs to work with both classes and instances.
        try:
            name = resource.__name__
        except AttributeError:
            name = resource.__class__.__name__

        message = f'The {method} method is not supported for {resource.__module__}.{name}'
        super().__init__(message=message)


class DuplicateResource(SDKException):
    """More than one resource exists with that name."""


class ResourceTimeout(SDKException):
    """Timeout waiting for resource."""


class ResourceFailure(SDKException):
    """General resource failure."""


class InvalidResourceQuery(SDKException):
    """Invalid query params for resource."""


def _extract_message(obj: ty.Any) -> ty.Optional[str]:
    if isinstance(obj, dict):
        # Most of services: compute, network
        if obj.get('message'):
            return str(obj['message'])
        # Ironic starting with Stein
        elif obj.get('faultstring'):
            return str(obj['faultstring'])
    elif isinstance(obj, str):
        # Ironic before Stein has double JSON encoding, nobody remembers why.
        try:
            obj = json.loads(obj)
        except Exception:
            pass
        else:
            return _extract_message(obj)
    return None


def raise_from_response(
    response: requests.Response,
    error_message: ty.Optional[str] = None,
) -> None:
    """Raise an instance of an HTTPException based on keystoneauth response."""
    if response.status_code < 400:
        return

    cls: type[SDKException]
    if response.status_code == 400:
        cls = BadRequestException
    elif response.status_code == 403:
        cls = ForbiddenException
    elif response.status_code == 404:
        cls = NotFoundException
    elif response.status_code == 409:
        cls = ConflictException
    elif response.status_code == 412:
        cls = PreconditionFailedException
    else:
        cls = HttpException

    details = None
    content_type = response.headers.get('content-type', '')
    if response.content and 'application/json' in content_type:
        # Iterate over the nested objects to retrieve "message" attribute.
        # TODO(shade) Add exception handling for times when the content type
        # is lying.

        try:
            content = response.json()
            messages = [_extract_message(obj) for obj in content.values()]
            if not any(messages):
                # Exception dict may be the root dict in projects that use WSME
                messages = [_extract_message(content)]
            # Join all of the messages together nicely and filter out any
            # objects that don't have a "message" attr.
            details = '\n'.join(msg for msg in messages if msg)
        except Exception:
            details = response.text
    elif response.content and 'text/html' in content_type:
        messages = []
        for line in response.text.splitlines():
            message = re.sub(r'<.+?>', '', line.strip())
            if message not in messages:
                messages.append(message)

        # Return joined string separated by colons.
        details = ': '.join(msg for msg in messages if msg)

    if not details:
        details = response.reason if response.reason else response.text

    http_status = response.status_code
    request_id = response.headers.get('x-openstack-request-id')

    raise cls(
        message=error_message,
        response=response,
        details=details,
        http_status=http_status,
        request_id=request_id,
    )


class ConfigException(SDKException):
    """Something went wrong with parsing your OpenStack Config."""


class NotSupported(SDKException):
    """Request cannot be performed by any supported API version."""


class ValidationException(SDKException):
    """Validation failed for resource."""


class ServiceDisabledException(ConfigException):
    """This service is disabled for reasons."""


class ServiceDiscoveryException(SDKException):
    """The service cannot be discovered."""


# Backwards compatibility
OpenStackCloudException = SDKException
ResourceNotFound = NotFoundException