~registry/neutron/github

« back to all changes in this revision

Viewing changes to common/lib/quantum/common/exceptions.py

  • Committer: Brad Hall
  • Date: 2011-11-28 18:33:52 UTC
  • Revision ID: git-v1:6a08320031d03913981c444cce97c7ccd08c8169
Second round of packaging changes

This change condenses the directory structure to something more similar to
what we had before while producing similar packages.

It also introduces version.py which allows us to get the version from git tags
(or a fallback version if not available).

Fixes lp bug 889336
Fixes lp bug 888795

Change-Id: I86136bd9dbabb5eb1f8366ed665ed9b54f695124

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
 
 
3
 
# Copyright 2011 Nicira Networks, Inc
4
 
# All Rights Reserved.
5
 
#
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
9
 
#
10
 
#         http://www.apache.org/licenses/LICENSE-2.0
11
 
#
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
16
 
#    under the License.
17
 
 
18
 
"""
19
 
Quantum base exception handling, including decorator for re-raising
20
 
Quantum-type exceptions. SHOULD include dedicated exception logging.
21
 
"""
22
 
 
23
 
import logging
24
 
import gettext
25
 
 
26
 
gettext.install('quantum', unicode=1)
27
 
 
28
 
 
29
 
class QuantumException(Exception):
30
 
    """Base Quantum Exception
31
 
 
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.
36
 
 
37
 
    """
38
 
    message = _("An unknown exception occurred.")
39
 
 
40
 
    def __init__(self, **kwargs):
41
 
        try:
42
 
            self._error_string = self.message % kwargs
43
 
 
44
 
        except Exception:
45
 
            # at least get the core message out if something happened
46
 
            self._error_string = self.message
47
 
 
48
 
    def __str__(self):
49
 
        return self._error_string
50
 
 
51
 
 
52
 
class ProcessExecutionError(IOError):
53
 
    def __init__(self, stdout=None, stderr=None, exit_code=None, cmd=None,
54
 
                 description=None):
55
 
        if description is None:
56
 
            description = "Unexpected error while running command."
57
 
        if exit_code is None:
58
 
            exit_code = '-'
59
 
        message = "%s\nCommand: %s\nExit code: %s\nStdout: %r\nStderr: %r" % (
60
 
                  description, cmd, exit_code, stdout, stderr)
61
 
        IOError.__init__(self, message)
62
 
 
63
 
 
64
 
class Error(Exception):
65
 
    def __init__(self, message=None):
66
 
        super(Error, self).__init__(message)
67
 
 
68
 
 
69
 
class ApiError(Error):
70
 
    def __init__(self, message='Unknown', code='Unknown'):
71
 
        self.message = message
72
 
        self.code = code
73
 
        super(ApiError, self).__init__('%s: %s' % (code, message))
74
 
 
75
 
 
76
 
class NotFound(QuantumException):
77
 
    pass
78
 
 
79
 
 
80
 
class ClassNotFound(NotFound):
81
 
    message = _("Class %(class_name)s could not be found")
82
 
 
83
 
 
84
 
class NetworkNotFound(NotFound):
85
 
    message = _("Network %(net_id)s could not be found")
86
 
 
87
 
 
88
 
class PortNotFound(NotFound):
89
 
    message = _("Port %(port_id)s could not be found " \
90
 
                "on network %(net_id)s")
91
 
 
92
 
 
93
 
class StateInvalid(QuantumException):
94
 
    message = _("Unsupported port state: %(port_state)s")
95
 
 
96
 
 
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.")
100
 
 
101
 
 
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.")
106
 
 
107
 
 
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")
112
 
 
113
 
 
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
116
 
# those scenarios
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.")
121
 
 
122
 
 
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.")
127
 
 
128
 
 
129
 
class Duplicate(Error):
130
 
    pass
131
 
 
132
 
 
133
 
class NotAuthorized(Error):
134
 
    pass
135
 
 
136
 
 
137
 
class NotEmpty(Error):
138
 
    pass
139
 
 
140
 
 
141
 
class Invalid(Error):
142
 
    pass
143
 
 
144
 
 
145
 
class InvalidContentType(Invalid):
146
 
    message = _("Invalid content type %(content_type)s.")
147
 
 
148
 
 
149
 
class BadInputError(Exception):
150
 
    """Error resulting from a client sending bad input to a server"""
151
 
    pass
152
 
 
153
 
 
154
 
class MissingArgumentError(Error):
155
 
    pass
156
 
 
157
 
 
158
 
def wrap_exception(f):
159
 
    def _wrap(*args, **kw):
160
 
        try:
161
 
            return f(*args, **kw)
162
 
        except Exception, e:
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))
167
 
                raise Error(str(e))
168
 
            raise
169
 
    _wrap.func_name = f.func_name
170
 
    return _wrap