Replace ex.message with exception_to_unicode(ex)

The message attribute of exceptions has been removed in Python 3. Use
exception_to_unicode(exc) of oslo_utils.encodeutils instead to get
the error message.

translate_exception(exc) of heat.common.wsgi tries to translate the
error message and store the translated message into exc.message.
Because of that, exc.message is kept for HeatException and
subclasses.

Co-Authored-By: Roman Podoliaka <rpodolyaka@mirantis.com>

Related-Bug: 1542961

Change-Id: If5a7514131f4a51c211237238bd11b79becec419
This commit is contained in:
Victor Stinner 2016-02-08 16:13:57 +01:00 committed by Roman Podoliaka
parent b0283e1da7
commit aa0ce43325
7 changed files with 43 additions and 11 deletions

View File

@ -36,6 +36,7 @@ from oslo_config import cfg
import oslo_i18n as i18n
from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
from oslo_utils import importutils
from paste import deploy
import routes
@ -957,7 +958,8 @@ def translate_exception(exc, locale):
if isinstance(exc, exception.HeatException):
exc.message = i18n.translate(exc.message, locale)
else:
exc.message = i18n.translate(six.text_type(exc), locale)
err_msg = encodeutils.exception_to_unicode(exc)
exc.message = i18n.translate(err_msg, locale)
if isinstance(exc, webob.exc.HTTPError):
exc.explanation = i18n.translate(exc.explanation, locale)

View File

@ -19,6 +19,7 @@ from oslo_config import cfg
from oslo_db.sqlalchemy import session as db_session
from oslo_db.sqlalchemy import utils
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
from oslo_utils import timeutils
import osprofiler.sqlalchemy
import six
@ -398,7 +399,8 @@ def _paginate_query(context, query, model, limit=None, sort_keys=None,
query = utils.paginate_query(query, model, limit, sort_keys,
model_marker, sort_dir)
except utils.InvalidSortKey as exc:
raise exception.Invalid(reason=exc.message)
err_msg = encodeutils.exception_to_unicode(exc)
raise exception.Invalid(reason=err_msg)
return query
@ -736,7 +738,8 @@ def _events_paginate_query(context, query, model, limit=None, sort_keys=None,
query = utils.paginate_query(query, model, limit, sort_keys,
model_marker, sort_dir)
except utils.InvalidSortKey as exc:
raise exception.Invalid(reason=exc.message)
err_msg = encodeutils.exception_to_unicode(exc)
raise exception.Invalid(reason=err_msg)
return query

View File

@ -12,6 +12,7 @@
# under the License.
from oslo_log import log as logging
from oslo_utils import encodeutils
import six
from heat.common import exception
@ -245,10 +246,11 @@ class ManilaShare(resource.Resource):
access_level=rule.get(self.ACCESS_LEVEL))
return True
except Exception as ex:
err_msg = encodeutils.exception_to_unicode(ex)
reason = _(
'Error during applying access rules to share "{0}". '
'The root cause of the problem is the following: {1}.'
).format(self.resource_id, ex.message)
).format(self.resource_id, err_msg)
raise exception.ResourceInError(status_reason=reason)
elif share_status == self.STATUS_ERROR:
reason = _('Error during creation of share "{0}"').format(

View File

@ -17,6 +17,7 @@ import re
import six
from oslo_log import log as logging
from oslo_utils import encodeutils
from heat.common import exception
from heat.common.i18n import _
@ -316,16 +317,16 @@ class SaharaNodeGroupTemplate(resource.Resource):
except Exception as ex:
if (self.client_plugin('neutron').is_not_found(ex)
or self.client_plugin('neutron').is_no_unique(ex)):
raise exception.StackValidationFailed(
message=ex.message)
err_msg = encodeutils.exception_to_unicode(ex)
raise exception.StackValidationFailed(message=err_msg)
raise
else:
try:
self.client('nova').floating_ip_pools.find(name=pool)
except Exception as ex:
if self.client_plugin('nova').is_not_found(ex):
raise exception.StackValidationFailed(
message=ex.message)
err_msg = encodeutils.exception_to_unicode(ex)
raise exception.StackValidationFailed(message=err_msg)
raise
self.client_plugin().validate_hadoop_version(

View File

@ -20,6 +20,7 @@ from novaclient import client as nc
from novaclient import exceptions as nova_exceptions
from oslo_config import cfg
from oslo_serialization import jsonutils as json
from oslo_utils import encodeutils
import six
from heat.common import exception
@ -624,7 +625,7 @@ class ConsoleUrlsTest(common.HeatTestCase):
urls = self.nova_plugin.get_console_urls(self.server)
e = self.assertRaises(exc, urls.__getitem__, self.console_type)
self.assertIn('spam', e.message)
self.assertIn('spam', encodeutils.exception_to_unicode(e))
self.console_method.assert_called_once_with(self.console_type)
def test_get_console_urls_reraises_other(self):

View File

@ -153,6 +153,22 @@ class SqlAlchemyTest(common.HeatTestCase):
self.assertTrue(mock_events_paginate_query.called)
@mock.patch.object(db_api.utils, 'paginate_query')
def test_events_filter_invalid_sort_key(self, mock_paginate_query):
query = mock.Mock()
class InvalidSortKey(db_api.utils.InvalidSortKey):
@property
def message(_):
self.fail("_events_paginate_query() should not have tried to "
"access .message attribute - it's deprecated in "
"oslo.db and removed from base Exception in Py3K.")
mock_paginate_query.side_effect = InvalidSortKey()
self.assertRaises(exception.Invalid,
db_api._events_filter_and_page_query,
self.ctx, query, sort_keys=['foo'])
@mock.patch.object(db_api.db_filters, 'exact_filter')
def test_filter_and_page_query_handles_no_filters(self, mock_db_filter):
query = mock.Mock()
@ -255,7 +271,14 @@ class SqlAlchemyTest(common.HeatTestCase):
query = mock.Mock()
model = mock.Mock()
mock_paginate_query.side_effect = db_api.utils.InvalidSortKey()
class InvalidSortKey(db_api.utils.InvalidSortKey):
@property
def message(_):
self.fail("_paginate_query() should not have tried to access "
".message attribute - it's deprecated in oslo.db "
"and removed from base Exception class in Py3K.")
mock_paginate_query.side_effect = InvalidSortKey()
self.assertRaises(exception.Invalid, db_api._paginate_query,
self.ctx, query, model, sort_keys=['foo'])

View File

@ -202,7 +202,7 @@ class SaharaNodeGroupTemplateTest(common.HeatTestCase):
self.patchobject(nova.NovaClientPlugin,
'_create').return_value = nova_mock
ex = self.assertRaises(exception.StackValidationFailed, ngt.validate)
self.assertEqual('Not found', six.text_type(ex))
self.assertEqual('Not found (HTTP 404)', six.text_type(ex))
def test_validate_flavor_constraint_return_false(self):
self.t['resources']['node-group']['properties'].pop('floating_ip_pool')