File: exceptions.py

package info (click to toggle)
python-tackerclient 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,456 kB
  • sloc: python: 11,602; makefile: 23; sh: 3
file content (186 lines) | stat: -rw-r--r-- 5,118 bytes parent folder | download | duplicates (2)
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
# Copyright 2011 VMware, Inc
# All Rights Reserved.
#
#    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.

from tackerclient.i18n import _

"""
Tacker base exception handling.

Exceptions are classified into three categories:
* Exceptions corresponding to exceptions from tacker server:
  This type of exceptions should inherit one of exceptions
  in HTTP_EXCEPTION_MAP.
* Exceptions from client library:
  This type of exceptions should inherit TackerClientException.
* Exceptions from CLI code:
  This type of exceptions should inherit TackerCLIError.
"""


class TackerException(Exception):
    """Base Tacker Exception.

    To correctly use this class, inherit from it and define
    a 'message' property. That message will get printf'd
    with the keyword arguments provided to the constructor.
    """
    message = _("An unknown exception occurred.")

    def __init__(self, message=None, **kwargs):
        if message:
            self.message = message
        try:
            self._error_string = self.message % kwargs
        except Exception:
            # at least get the core message out if something happened
            self._error_string = self.message

    def __str__(self):
        return self._error_string


class TackerClientException(TackerException):
    """Base exception which exceptions from Tacker are mapped into.

    NOTE: on the client side, we use different exception types in order
    to allow client library users to handle server exceptions in try...except
    blocks. The actual error message is the one generated on the server side.
    """

    status_code = 0

    def __init__(self, message=None, **kwargs):
        if 'status_code' in kwargs:
            self.status_code = kwargs['status_code']
        super(TackerClientException, self).__init__(message, **kwargs)


# Base exceptions from Tacker

class BadRequest(TackerClientException):
    status_code = 400


class Unauthorized(TackerClientException):
    status_code = 401
    message = _("Unauthorized: bad credentials.")


class Forbidden(TackerClientException):
    status_code = 403
    message = _("Forbidden: your credentials don't give you access to this "
                "resource.")


class NotFound(TackerClientException):
    status_code = 404


class Conflict(TackerClientException):
    status_code = 409


class InternalServerError(TackerClientException):
    status_code = 500


class ServiceUnavailable(TackerClientException):
    status_code = 503


HTTP_EXCEPTION_MAP = {
    400: BadRequest,
    401: Unauthorized,
    403: Forbidden,
    404: NotFound,
    409: Conflict,
    500: InternalServerError,
    503: ServiceUnavailable,
}


# Exceptions from client library

class NoAuthURLProvided(Unauthorized):
    message = _("auth_url was not provided to the Tacker client")


class EndpointNotFound(TackerClientException):
    message = _("Could not find Service or Region in Service Catalog.")


class EndpointTypeNotFound(TackerClientException):
    message = _("Could not find endpoint type %(type_)s in Service Catalog.")


class RequestURITooLong(TackerClientException):
    """Raised when a request fails with HTTP error 414."""

    def __init__(self, **kwargs):
        self.excess = kwargs.get('excess', 0)
        super(RequestURITooLong, self).__init__(**kwargs)


class ConnectionFailed(TackerClientException):
    message = _("Connection to tacker failed: %(reason)s")


class SslCertificateValidationError(TackerClientException):
    message = _("SSL certificate validation has failed: %(reason)s")


class MalformedResponseBody(TackerClientException):
    message = _("Malformed response body: %(reason)s")


class InvalidContentType(TackerClientException):
    message = _("Invalid content type %(content_type)s.")


class InvalidInput(TackerClientException):
    message = _("Invalid input: %(reason)s")


class EmptyInput(TackerClientException):
    message = _("Empty input: %(reason)s")


class UnsupportedCommandVersion(TackerClientException):
    message = _("This command is not supported in version %(version)s")


# Command line exceptions

class TackerCLIError(TackerException):
    """Exception raised when command line parsing fails."""
    pass


class CommandError(TackerCLIError):
    pass


class UnsupportedVersion(TackerCLIError):
    """Unsupported Version.

    Indicates that the user is trying to use an unsupported version of
    the API.
    """
    pass


class TackerClientNoUniqueMatch(TackerCLIError):
    message = _("Multiple %(resource)s matches found for name '%(name)s',"
                " use an ID to be more specific.")