From 02d017022bab577b581b58f42b468bf0b2c644a9 Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Fri, 13 Dec 2013 08:13:45 +0100 Subject: [PATCH] Ensure the correct error message is displayed When a untranslated error message pass through the translation pecan hook, the previous translated error is send to the client. This patch fix that by reseting the "translatable_error" of the translation pecan hook before each request. Change-Id: Icee79cf09fd696655048c7c0cf8a3d6706e31701 Fixes: bug #1260398 --- ceilometer/api/hooks.py | 3 +++ ceilometer/tests/api/v2/test_app.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/ceilometer/api/hooks.py b/ceilometer/api/hooks.py index bcd4cd1c8..f4538caff 100644 --- a/ceilometer/api/hooks.py +++ b/ceilometer/api/hooks.py @@ -74,6 +74,9 @@ class TranslationHook(hooks.PecanHook): self.local_error = threading.local() self.local_error.translatable_error = None + def before(self, state): + self.local_error.translatable_error = None + def after(self, state): if hasattr(state.response, 'translatable_error'): self.local_error.translatable_error = ( diff --git a/ceilometer/tests/api/v2/test_app.py b/ceilometer/tests/api/v2/test_app.py index d2867957e..30232fa07 100644 --- a/ceilometer/tests/api/v2/test_app.py +++ b/ceilometer/tests/api/v2/test_app.py @@ -18,9 +18,11 @@ # under the License. """Test basic ceilometer-api app """ +import json import os import mock +import wsme from ceilometer.api import acl from ceilometer.api import app @@ -207,3 +209,19 @@ class TestApiMiddleware(FunctionalTest): fault = response.xml.findall('./error/faultstring') for fault_string in fault: self.assertEqual(fault_string.text, self.en_US_translated_error) + + def test_translated_then_untranslated_error(self): + resp = self.get_json('/alarms/alarm-id-3', expect_errors=True) + self.assertEqual(resp.status_code, 404) + self.assertEqual(json.loads(resp.body)['error_message'] + ['faultstring'], "Alarm alarm-id-3 Not Found") + + with mock.patch('ceilometer.api.controllers.v2.EntityNotFound') \ + as CustomErrorClass: + CustomErrorClass.return_value = wsme.exc.ClientSideError( + "untranslated_error", status_code=404) + resp = self.get_json('/alarms/alarm-id-5', expect_errors=True) + + self.assertEqual(resp.status_code, 404) + self.assertEqual(json.loads(resp.body)['error_message'] + ['faultstring'], "untranslated_error")