Skip to content

Commit

Permalink
Merge "Refactoring the exceptions lists"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Apr 6, 2015
2 parents 7b61b61 + 7fc1baa commit c3fc267
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 121 deletions.
6 changes: 0 additions & 6 deletions horizon/conf/__init__.py
Expand Up @@ -23,12 +23,6 @@ def _setup(self, name=None):
HORIZON_CONFIG = copy.copy(DEFAULT_CONFIG)
HORIZON_CONFIG.update(settings.HORIZON_CONFIG)

# Ensure we always have our exception configuration...
for exc_category in ['unauthorized', 'not_found', 'recoverable']:
if exc_category not in HORIZON_CONFIG['exceptions']:
default_exc_config = DEFAULT_CONFIG['exceptions'][exc_category]
HORIZON_CONFIG['exceptions'][exc_category] = default_exc_config

# Ensure our password validator always exists...
if 'regex' not in HORIZON_CONFIG['password_validator']:
default_pw_regex = DEFAULT_CONFIG['password_validator']['regex']
Expand Down
5 changes: 0 additions & 5 deletions horizon/conf/default.py
Expand Up @@ -31,11 +31,6 @@
# URL for additional help with this site.
'help_url': None,

# Exception configuration.
'exceptions': {'unauthorized': [],
'not_found': [],
'recoverable': []},

# Password configuration.
'password_validator': {'regex': '.*',
'help_text': _("Password is not accepted")},
Expand Down
28 changes: 14 additions & 14 deletions horizon/exceptions.py
Expand Up @@ -22,14 +22,14 @@

import six

from django.conf import settings
from django.core.management import color_style # noqa
from django.http import HttpRequest # noqa
from django.utils import encoding
from django.utils.translation import ugettext_lazy as _
from django.views.debug import CLEANSED_SUBSTITUTE # noqa
from django.views.debug import SafeExceptionReporterFilter # noqa

from horizon.conf import HORIZON_CONFIG # noqa
from horizon import messages

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -202,12 +202,6 @@ def __init__(self, wrapped):
self.wrapped = wrapped


UNAUTHORIZED = tuple(HORIZON_CONFIG['exceptions']['unauthorized'])
NOT_FOUND = tuple(HORIZON_CONFIG['exceptions']['not_found'])
RECOVERABLE = (AlreadyExists, Conflict, NotAvailable, ServiceCatalogException)
RECOVERABLE += tuple(HORIZON_CONFIG['exceptions']['recoverable'])


def error_color(msg):
return color_style().ERROR_OUTPUT(msg)

Expand Down Expand Up @@ -280,13 +274,6 @@ def handle_recoverable(request, message, redirect, ignore, escalate, handled,
return RecoverableError # return to normal code flow


HANDLE_EXC_METHODS = [
{'exc': UNAUTHORIZED, 'handler': handle_unauthorized, 'set_wrap': False},
{'exc': NOT_FOUND, 'handler': handle_notfound, 'set_wrap': True},
{'exc': RECOVERABLE, 'handler': handle_recoverable, 'set_wrap': True},
]


def handle(request, message=None, redirect=None, ignore=False,
escalate=False, log_level=None, force_log=None):
"""Centralized error handling for Horizon.
Expand Down Expand Up @@ -316,6 +303,19 @@ def handle(request, message=None, redirect=None, ignore=False,
class indicating the type of exception that was encountered will be
returned.
"""
HORIZON_CONFIG = settings.HORIZON_CONFIG
UNAUTHORIZED = tuple(HORIZON_CONFIG['exceptions']['unauthorized'])
NOT_FOUND = tuple(HORIZON_CONFIG['exceptions']['not_found'])
RECOVERABLE = (AlreadyExists, Conflict, NotAvailable,
ServiceCatalogException)
RECOVERABLE += tuple(HORIZON_CONFIG['exceptions']['recoverable'])
HANDLE_EXC_METHODS = [
{'exc': UNAUTHORIZED, 'handler': handle_unauthorized,
'set_wrap': False},
{'exc': NOT_FOUND, 'handler': handle_notfound, 'set_wrap': True},
{'exc': RECOVERABLE, 'handler': handle_recoverable, 'set_wrap': True}
]

exc_type, exc_value, exc_traceback = sys.exc_info()
log_method = getattr(LOG, log_level or "exception")
force_log = force_log or os.environ.get("HORIZON_TEST_RUN", False)
Expand Down
42 changes: 42 additions & 0 deletions horizon/test/settings.py
Expand Up @@ -20,6 +20,17 @@
import socket
import sys

from cinderclient import exceptions as cinderclient
from glanceclient.common import exceptions as glanceclient
from heatclient import exc as heatclient
from keystoneclient import exceptions as keystoneclient
from neutronclient.common import exceptions as neutronclient
from novaclient import exceptions as novaclient
from requests import exceptions as requests
from saharaclient.api import base as saharaclient
from swiftclient import client as swiftclient
from troveclient import exceptions as troveclient

import django
from django.utils import html_parser
from openstack_dashboard.static_settings import get_staticfiles_dirs # noqa
Expand Down Expand Up @@ -135,6 +146,37 @@
},
'user_home': None,
'help_url': "http://example.com",
'exceptions': {'recoverable': (keystoneclient.ClientException,
keystoneclient.AuthorizationFailure,
keystoneclient.Forbidden,
cinderclient.ClientException,
cinderclient.ConnectionError,
cinderclient.Forbidden,
novaclient.ClientException,
novaclient.Forbidden,
glanceclient.ClientException,
neutronclient.Forbidden,
neutronclient.NeutronClientException,
swiftclient.ClientException,
heatclient.HTTPForbidden,
heatclient.HTTPException,
troveclient.ClientException,
saharaclient.APIException,
requests.RequestException),
'not_found': (keystoneclient.NotFound,
cinderclient.NotFound,
novaclient.NotFound,
glanceclient.NotFound,
neutronclient.NotFound,
heatclient.HTTPNotFound,
troveclient.NotFound),
'unauthorized': (keystoneclient.Unauthorized,
cinderclient.Unauthorized,
novaclient.Unauthorized,
glanceclient.Unauthorized,
neutronclient.Unauthorized,
heatclient.HTTPUnauthorized,
troveclient.Unauthorized)}
}

COMPRESS_ENABLED = True
Expand Down
18 changes: 7 additions & 11 deletions openstack_dashboard/api/rest/utils.py
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import functools
import itertools
import json
import logging

Expand All @@ -21,7 +22,6 @@

from oslo_serialization import jsonutils

from horizon import exceptions

log = logging.getLogger(__name__)

Expand All @@ -31,9 +31,6 @@ def __init__(self, http_status, msg):
self.http_status = http_status
super(AjaxError, self).__init__(msg)

http_errors = exceptions.UNAUTHORIZED + exceptions.NOT_FOUND + \
exceptions.RECOVERABLE + (AjaxError, )


class CreatedResponse(http.HttpResponse):
def __init__(self, location, data=None):
Expand Down Expand Up @@ -110,25 +107,24 @@ def _wrapped(self, request, *args, **kw):
return JSONResponse('request requires JSON body', 400)

# invoke the wrapped function, handling exceptions sanely
horizon_exc = settings.HORIZON_CONFIG['exceptions'].values()
api_exc = itertools.chain([AjaxError, ], *horizon_exc)
try:
data = function(self, request, *args, **kw)
if isinstance(data, http.HttpResponse):
return data
elif data is None:
return JSONResponse('', status=204)
return JSONResponse(data)
except http_errors as e:
# exception was raised with a specific HTTP status
except tuple(api_exc) as e:
if hasattr(e, 'http_status'):
http_status = e.http_status
elif hasattr(e, 'code'):
http_status = e.code
else:
log.exception('HTTP exception with no status/code')
return JSONResponse(str(e), 500)
http_status = getattr(e, 'code', 500)
log.exception('API Error: %s', e)
return JSONResponse(str(e), http_status)
except Exception as e:
log.exception('error invoking apiclient')
log.exception('Internal Error: %s', e)
return JSONResponse(str(e), 500)

return _wrapped
Expand Down
73 changes: 0 additions & 73 deletions openstack_dashboard/exceptions.py

This file was deleted.

7 changes: 3 additions & 4 deletions openstack_dashboard/local/local_settings.py.example
Expand Up @@ -2,7 +2,6 @@ import os

from django.utils.translation import ugettext_lazy as _

from openstack_dashboard import exceptions

DEBUG = True
TEMPLATE_DEBUG = DEBUG
Expand Down Expand Up @@ -74,9 +73,9 @@ HORIZON_CONFIG = {
'types': ['alert-success', 'alert-info']
},
'help_url': "http://docs.openstack.org",
'exceptions': {'recoverable': exceptions.RECOVERABLE,
'not_found': exceptions.NOT_FOUND,
'unauthorized': exceptions.UNAUTHORIZED},
'exceptions': {'recoverable': [],
'not_found': [],
'unauthorized': []},
'modal_backdrop': 'static',
'angular_modules': [],
'js_files': [],
Expand Down
46 changes: 42 additions & 4 deletions openstack_dashboard/settings.py
Expand Up @@ -21,10 +21,20 @@
import sys
import warnings

from cinderclient import exceptions as cinderclient
from glanceclient.common import exceptions as glanceclient
from heatclient import exc as heatclient
from keystoneclient import exceptions as keystoneclient
from neutronclient.common import exceptions as neutronclient
from novaclient import exceptions as novaclient
from requests import exceptions as requests
from saharaclient.api import base as saharaclient
from swiftclient import client as swiftclient
from troveclient import exceptions as troveclient

import django
from django.utils.translation import ugettext_lazy as _

from openstack_dashboard import exceptions
from openstack_dashboard.static_settings import get_staticfiles_dirs # noqa


Expand Down Expand Up @@ -59,9 +69,37 @@
'types': ['alert-success', 'alert-info']
},
'help_url': "http://docs.openstack.org",
'exceptions': {'recoverable': exceptions.RECOVERABLE,
'not_found': exceptions.NOT_FOUND,
'unauthorized': exceptions.UNAUTHORIZED},
'exceptions': {'recoverable': (keystoneclient.ClientException,
keystoneclient.AuthorizationFailure,
keystoneclient.Forbidden,
cinderclient.ClientException,
cinderclient.ConnectionError,
cinderclient.Forbidden,
novaclient.ClientException,
novaclient.Forbidden,
glanceclient.ClientException,
neutronclient.Forbidden,
neutronclient.NeutronClientException,
swiftclient.ClientException,
heatclient.HTTPForbidden,
heatclient.HTTPException,
troveclient.ClientException,
saharaclient.APIException,
requests.RequestException),
'not_found': (keystoneclient.NotFound,
cinderclient.NotFound,
novaclient.NotFound,
glanceclient.NotFound,
neutronclient.NotFound,
heatclient.HTTPNotFound,
troveclient.NotFound),
'unauthorized': (keystoneclient.Unauthorized,
cinderclient.Unauthorized,
novaclient.Unauthorized,
glanceclient.Unauthorized,
neutronclient.Unauthorized,
heatclient.HTTPUnauthorized,
troveclient.Unauthorized)},
'angular_modules': [],
'js_files': [],
'js_spec_files': [],
Expand Down

0 comments on commit c3fc267

Please sign in to comment.