From c66c23ec4e7a6ce85370de89a1befa8e6250ab60 Mon Sep 17 00:00:00 2001 From: Jonas Pfannschmidt Date: Thu, 10 Mar 2016 13:39:24 +0000 Subject: [PATCH] Use correct parameter when handling exceptions Before this fix, error handling code passed the view rather than the request object to exeptions.handle(). This resulted in an exception in the error handling code which made the actual error harder to identify. Also added a check to see if the redirect goes to the same page which would result in an endless loop. Fixes bug: 1555642 Change-Id: Ie941ff4d595a375e5fa808703b146e86d4d56e1f --- disaster_recovery/utils.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/disaster_recovery/utils.py b/disaster_recovery/utils.py index c7bff13..276a641 100644 --- a/disaster_recovery/utils.py +++ b/disaster_recovery/utils.py @@ -23,6 +23,7 @@ from django.template.defaultfilters import date as django_date from django.utils.translation import ugettext_lazy as _ from horizon import exceptions +from horizon import get_user_home LOG = logging.getLogger(__name__) @@ -182,15 +183,23 @@ def shield(message, redirect=''): def wrap(function): @wraps(function) - def wrapped_function(request, *args, **kwargs): + def wrapped_function(view, *args, **kwargs): try: - return function(request, *args, **kwargs) + return function(view, *args, **kwargs) except Exception as error: LOG.error(error.message) namespace = "horizon:disaster_recovery:" r = reverse("{0}{1}".format(namespace, redirect)) - exceptions.handle(request, _(message), redirect=r) + + if view.request.path == r: + # To avoid an endless loop, we must not redirect to the + # same page on which the error happened + user_home = get_user_home(view.request.user) + exceptions.handle(view.request, _(message), + redirect=user_home) + else: + exceptions.handle(view.request, _(message), redirect=r) return wrapped_function return wrap