~ubuntu-branches/ubuntu/raring/horizon/raring

« back to all changes in this revision

Viewing changes to horizon/exceptions.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-24 14:33:20 UTC
  • mfrom: (1.1.14)
  • Revision ID: package-import@ubuntu.com-20120524143320-i7eswfq6ecxlvh5a
Tags: 2012.2~f1-0ubuntu1
* New usptream release. 
* Prepare for quantal:
  - debian/patches/fix-coverage-binary-name.patch: Refreshed.
* Temporarily pass the testsuite.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
"""
20
20
 
21
21
import logging
 
22
import os
22
23
import sys
23
24
 
24
25
from django.conf import settings
25
26
from django.contrib import messages
 
27
from django.utils import termcolors
26
28
from django.utils.translation import ugettext as _
27
29
from cloudfiles import errors as swiftclient
28
 
from glance.common import exception as glanceclient
 
30
from glanceclient.common import exceptions as glanceclient
29
31
from keystoneclient import exceptions as keystoneclient
30
32
from novaclient import exceptions as novaclient
31
33
 
32
34
 
33
35
LOG = logging.getLogger(__name__)
 
36
PALETTE = termcolors.PALETTES[termcolors.DEFAULT_PALETTE]
34
37
 
35
38
 
36
39
class HorizonException(Exception):
106
109
    def __repr__(self):
107
110
        return self.msg % self.attrs
108
111
 
 
112
    def __str__(self):
 
113
        return self.msg % self.attrs
 
114
 
109
115
    def __unicode__(self):
110
116
        return _(self.msg) % self.attrs
111
117
 
112
118
 
 
119
class WorkflowError(HorizonException):
 
120
    """ Exception to be raised when something goes wrong in a workflow. """
 
121
    pass
 
122
 
 
123
 
 
124
class WorkflowValidationError(HorizonException):
 
125
    """
 
126
    Exception raised during workflow validation if required data is missing,
 
127
    or existing data is not valid.
 
128
    """
 
129
    pass
 
130
 
 
131
 
113
132
class HandledException(HorizonException):
114
133
    """
115
134
    Used internally to track exceptions that have gone through
128
147
                novaclient.Unauthorized,
129
148
                novaclient.Forbidden,
130
149
                glanceclient.AuthorizationFailure,
131
 
                glanceclient.NotAuthorized,
 
150
                glanceclient.Unauthorized,
132
151
                swiftclient.AuthenticationFailed,
133
152
                swiftclient.AuthenticationError)
134
153
UNAUTHORIZED += tuple(EXCEPTION_CONFIG.get('unauthorized', []))
146
165
               # AuthorizationFailure is raised when Keystone is "unavailable".
147
166
               keystoneclient.AuthorizationFailure,
148
167
               novaclient.ClientException,
149
 
               glanceclient.GlanceException,
 
168
               glanceclient.ClientException,
150
169
               swiftclient.Error,
151
170
               AlreadyExists)
152
171
RECOVERABLE += tuple(EXCEPTION_CONFIG.get('recoverable', []))
153
172
 
154
173
 
155
 
def handle(request, message=None, redirect=None, ignore=False, escalate=False):
 
174
def _error_color(msg):
 
175
    return termcolors.colorize(msg, **PALETTE['ERROR'])
 
176
 
 
177
 
 
178
def handle(request, message=None, redirect=None, ignore=False,
 
179
           escalate=False, log_level=None, force_log=None):
156
180
    """ Centralized error handling for Horizon.
157
181
 
158
182
    Because Horizon consumes so many different APIs with completely
181
205
    returned.
182
206
    """
183
207
    exc_type, exc_value, exc_traceback = sys.exc_info()
 
208
    log_method = getattr(LOG, log_level or "exception")
 
209
    force_log = force_log or os.environ.get("HORIZON_TEST_RUN", False)
 
210
    force_silence = getattr(exc_value, "silence_logging", False)
184
211
 
185
212
    # Because the same exception may travel through this method more than
186
213
    # once (if it's re-raised) we may want to treat it differently
204
231
        if ignore:
205
232
            return NotAuthorized
206
233
        request.user_logout()
 
234
        if not force_silence and not handled:
 
235
            log_method(_error_color("Unauthorized: %s" % exc_value))
207
236
        if not handled:
208
 
            LOG.debug("Unauthorized: %s" % exc_value)
209
237
            # We get some pretty useless error messages back from
210
238
            # some clients, so let's define our own fallback.
211
239
            fallback = _("Unauthorized. Please try logging in again.")
214
242
 
215
243
    if issubclass(exc_type, NOT_FOUND):
216
244
        wrap = True
 
245
        if not force_silence and not handled and (not ignore or force_log):
 
246
            log_method(_error_color("Not Found: %s" % exc_value))
217
247
        if not ignore and not handled:
218
 
            LOG.debug("Not Found: %s" % exc_value)
219
248
            messages.error(request, message or exc_value)
220
249
        if redirect:
221
250
            raise Http302(redirect)
224
253
 
225
254
    if issubclass(exc_type, RECOVERABLE):
226
255
        wrap = True
 
256
        if not force_silence and not handled and (not ignore or force_log):
 
257
            log_method(_error_color("Recoverable error: %s" % exc_value))
227
258
        if not ignore and not handled:
228
 
            LOG.debug("Recoverable error: %s" % exc_value)
229
259
            messages.error(request, message or exc_value)
230
260
        if redirect:
231
261
            raise Http302(redirect)