From a2124b8c7ef51620e0a8d55f276d9b3ad58cd867 Mon Sep 17 00:00:00 2001 From: Drago Rosson Date: Fri, 13 Mar 2015 17:52:44 +0000 Subject: [PATCH] Separate message and traceback even if not a remote error Faultwrap exposes tracebacks in error messages when Oslo.messaging cannot add a _remote suffix to the exception generated by the Heat engine. Change-Id: Iaf91dafacce1abe94fb10aa2ba380cafb6fc4a84 closes-bug: #1432726 --- heat/api/middleware/fault.py | 7 ++++++- heat/tests/test_fault_middleware.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/heat/api/middleware/fault.py b/heat/api/middleware/fault.py index e827c09cc..fe424c4a0 100644 --- a/heat/api/middleware/fault.py +++ b/heat/api/middleware/fault.py @@ -104,6 +104,7 @@ class FaultWrapper(wsgi.Middleware): def _error(self, ex): trace = None + traceback_marker = 'Traceback (most recent call last)' webob_exc = None if isinstance(ex, exception.HTTPExceptionDisguise): # An HTTP exception was disguised so it could make it here @@ -120,8 +121,12 @@ class FaultWrapper(wsgi.Middleware): ex_type = ex_type[:-len('_Remote')] full_message = six.text_type(ex) - if full_message.find('\n') > -1 and is_remote: + if '\n' in full_message and is_remote: message, msg_trace = full_message.split('\n', 1) + elif traceback_marker in full_message: + message, msg_trace = full_message.split(traceback_marker, 1) + message = message.rstrip('\n') + msg_trace = traceback_marker + msg_trace else: msg_trace = traceback.format_exc() message = full_message diff --git a/heat/tests/test_fault_middleware.py b/heat/tests/test_fault_middleware.py index 9be4fa7a4..c74d230d2 100644 --- a/heat/tests/test_fault_middleware.py +++ b/heat/tests/test_fault_middleware.py @@ -53,6 +53,21 @@ class FaultMiddlewareTest(common.HeatTestCase): 'title': 'Bad Request'} self.assertEqual(expected, msg) + def test_http_exception_with_traceback(self): + wrapper = fault.FaultWrapper(None) + newline_error = ErrorWithNewline( + 'Error with \n newline\nTraceback (most recent call last):\nFoo') + msg = wrapper._error(heat_exc.HTTPExceptionDisguise(newline_error)) + expected = {'code': 400, + 'error': {'message': 'Error with \n newline', + 'traceback': None, + 'type': 'ErrorWithNewline'}, + 'explanation': ('The server could not comply with the ' + 'request since it is either malformed ' + 'or otherwise incorrect.'), + 'title': 'Bad Request'} + self.assertEqual(expected, msg) + def test_openstack_exception_with_kwargs(self): wrapper = fault.FaultWrapper(None) msg = wrapper._error(heat_exc.StackNotFound(stack_name='a'))