horizon/horizon/test/tests/exceptions.py
David Lyle a5f5912bc3 Improve message propagation in exceptions.handle
In horizon/exceptions.py the handle(message="blah") function only
passes the message onto the user if the exception type is
HorizonException or a specifically handled class. In many cases,
including many forms, the except block calling this method is
catching the broad Exception and not more specific errors. Once
Exception is caught, we pass on an error message to
horizon/exceptions.handle(). That message is ignored in several cases
and the result is the underlying Django forms puts up a generic error
message instead: "Danger: There was an error submitting the form..." and
the redirect still happens.

This change treats exceptions where a message is passed in to handle as
recoverable as the exception has been caught and a user intended message
attached.

Closes-Bug: #1484723
Change-Id: I763f7606dde5df6cf09cbaf2c8e90158e0f256e3
2015-09-02 18:03:57 -06:00

63 lines
2.5 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from django.utils.encoding import force_text
from horizon import exceptions
from horizon.test import helpers as test
class HandleTests(test.TestCase):
def test_handle_translated(self):
translated_unicode = u'\u30b3\u30f3\u30c6\u30ca\u30fc\u304c' \
u'\u7a7a\u3067\u306f\u306a\u3044\u305f' \
u'\u3081\u3001\u524a\u9664\u3067\u304d' \
u'\u307e\u305b\u3093\u3002'
# Japanese translation of:
# 'Because the container is not empty, it can not be deleted.'
expected = ['error', force_text(translated_unicode), '']
req = self.request
req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
try:
raise exceptions.Conflict(translated_unicode)
except exceptions.Conflict:
exceptions.handle(req)
# The real test here is to make sure the handle method doesn't throw a
# UnicodeEncodeError, but making sure the message is correct could be
# useful as well.
self.assertItemsEqual(req.horizon['async_messages'], [expected])
def test_handle_message_as_recoverable(self):
# tests that if a message is passed to handle that it is treated
# like a recoverable exception
message = u"Couldn't make the thing"
exc_msg = u"Exception string"
req = self.request
req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
try:
raise Exception(exc_msg)
except Exception:
exceptions.handle(req, message)
# async_messages is a list of tuples, so [0][1] is getting to the
# message part of the first message. There should be only one message
# in this test case.
self.assertIn(message, req.horizon['async_messages'][0][1])
# verifying that the exec message which in this case is not trusted
# is not in the message content
self.assertNotIn(exc_msg, req.horizon['async_messages'][0][1])