1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 Nicira Networks, Inc
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
19
Quantum base exception handling, including decorator for re-raising
20
Quantum-type exceptions. SHOULD include dedicated exception logging.
26
gettext.install('quantum', unicode=1)
29
class QuantumException(Exception):
30
"""Base Quantum Exception
32
Taken from nova.exception.NovaException
33
To correctly use this class, inherit from it and define
34
a 'message' property. That message will get printf'd
35
with the keyword arguments provided to the constructor.
38
message = _("An unknown exception occurred.")
40
def __init__(self, **kwargs):
42
self._error_string = self.message % kwargs
45
# at least get the core message out if something happened
46
self._error_string = self.message
49
return self._error_string
52
class ProcessExecutionError(IOError):
53
def __init__(self, stdout=None, stderr=None, exit_code=None, cmd=None,
55
if description is None:
56
description = "Unexpected error while running command."
59
message = "%s\nCommand: %s\nExit code: %s\nStdout: %r\nStderr: %r" % (
60
description, cmd, exit_code, stdout, stderr)
61
IOError.__init__(self, message)
64
class Error(Exception):
65
def __init__(self, message=None):
66
super(Error, self).__init__(message)
69
class ApiError(Error):
70
def __init__(self, message='Unknown', code='Unknown'):
71
self.message = message
73
super(ApiError, self).__init__('%s: %s' % (code, message))
76
class NotFound(QuantumException):
80
class ClassNotFound(NotFound):
81
message = _("Class %(class_name)s could not be found")
84
class NetworkNotFound(NotFound):
85
message = _("Network %(net_id)s could not be found")
88
class PortNotFound(NotFound):
89
message = _("Port %(port_id)s could not be found " \
90
"on network %(net_id)s")
93
class StateInvalid(QuantumException):
94
message = _("Unsupported port state: %(port_state)s")
97
class NetworkInUse(QuantumException):
98
message = _("Unable to complete operation on network %(net_id)s. " \
99
"There is one or more attachments plugged into its ports.")
102
class PortInUse(QuantumException):
103
message = _("Unable to complete operation on port %(port_id)s " \
104
"for network %(net_id)s. The attachment '%(att_id)s" \
105
"is plugged into the logical port.")
108
class AlreadyAttached(QuantumException):
109
message = _("Unable to plug the attachment %(att_id)s into port " \
110
"%(port_id)s for network %(net_id)s. The attachment is " \
111
"already plugged into port %(att_port_id)s")
114
# NOTE: on the client side, we often do not know all of the information
115
# that is known on the server, thus, we create separate exception for
117
class PortInUseClient(QuantumException):
118
message = _("Unable to complete operation on port %(port_id)s " \
119
"for network %(net_id)s. An attachment " \
120
"is plugged into the logical port.")
123
class AlreadyAttachedClient(QuantumException):
124
message = _("Unable to plug the attachment %(att_id)s into port " \
125
"%(port_id)s for network %(net_id)s. The attachment is " \
126
"already plugged into another port.")
129
class Duplicate(Error):
133
class NotAuthorized(Error):
137
class NotEmpty(Error):
141
class Invalid(Error):
145
class InvalidContentType(Invalid):
146
message = _("Invalid content type %(content_type)s.")
149
class BadInputError(Exception):
150
"""Error resulting from a client sending bad input to a server"""
154
class MissingArgumentError(Error):
158
def wrap_exception(f):
159
def _wrap(*args, **kw):
161
return f(*args, **kw)
163
if not isinstance(e, Error):
164
#exc_type, exc_value, exc_traceback = sys.exc_info()
165
logging.exception('Uncaught exception')
166
#logging.error(traceback.extract_stack(exc_traceback))
169
_wrap.func_name = f.func_name