exceptions: provide .message attribute for Py3K compatibility

In two recent commits we fixed the way .message class attribute was
used by passing it as an argument to __init__() of the base Exception
class instead.

Unfortunately, this currently breaks Heat unit tests on Py3K as
exception instances no longer provide .message attribute there.

Re-introduce .message properties only for exception classes that used
to have them to ensure the new oslo.db release do not change the
interface of exceptions.

Closes-Bug: #1542961

Change-Id: I5cae408555956d77e6cbb4e5e513c1c6c375d29d
This commit is contained in:
Roman Podoliaka
2016-02-08 11:51:27 +02:00
parent 5f99361196
commit 029f0966b2
2 changed files with 21 additions and 0 deletions

View File

@@ -43,6 +43,7 @@ with `try/except` statement. This is required for consistent handling of
database errors.
"""
import debtcollector.removals
import six
from oslo_db._i18n import _
@@ -147,6 +148,13 @@ class DBInvalidUnicodeParameter(Exception):
without encoding directive.
"""
@debtcollector.removals.removed_property
def message(self):
# NOTE(rpodolyaka): provided for compatibility with python 3k, where
# exceptions do not have .message attribute, while we used to have one
# in this particular exception class. See LP #1542961 for details.
return str(self)
def __init__(self):
super(DBInvalidUnicodeParameter, self).__init__(
_("Invalid Parameter: Encoding directive wasn't provided."))
@@ -184,6 +192,13 @@ class DBDataError(DBError):
class InvalidSortKey(Exception):
"""A sort key destined for database query usage is invalid."""
@debtcollector.removals.removed_property
def message(self):
# NOTE(rpodolyaka): provided for compatibility with python 3k, where
# exceptions do not have .message attribute, while we used to have one
# in this particular exception class. See LP #1542961 for details.
return str(self)
def __init__(self, key=None):
super(InvalidSortKey, self).__init__(
_("Sort key supplied is invalid: %s") % key)

View File

@@ -153,11 +153,17 @@ class TestPaginateQuery(test_base.BaseTestCase):
str(exception.InvalidSortKey()))
self.assertEqual("Sort key supplied is invalid: lol",
str(exception.InvalidSortKey("lol")))
self.assertEqual("Sort key supplied is invalid: lol",
exception.InvalidSortKey("lol").message)
def test_invalid_unicode_paramater_str(self):
self.assertEqual(
"Invalid Parameter: Encoding directive wasn't provided.",
str(exception.DBInvalidUnicodeParameter()))
self.assertEqual(
"Invalid Parameter: Encoding directive wasn't provided.",
exception.DBInvalidUnicodeParameter().message
)
def test_paginate_query_attribute_error(self):
sqlalchemy.asc(self.model.user_id).AndReturn('asc')