diff options
author | Jenkins <jenkins@review.openstack.org> | 2013-08-22 18:36:13 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2013-08-22 18:36:13 +0000 |
commit | 35405fae3bc356adb7d435f0e1bd2f1ac7f5cf78 (patch) | |
tree | 834079335cb8caf050d9b45d82fe36d4b0b56219 | |
parent | 7f2def79e01a30eba152da23cfe14f42feae4f3f (diff) | |
parent | f14c18852f8a1f1af8b19a4b5327787d8f439158 (diff) | |
download | trove-35405fae3bc356adb7d435f0e1bd2f1ac7f5cf78.tar.gz |
Merge "Ensure safe format strings for TroveError"
-rw-r--r-- | trove/common/exception.py | 10 | ||||
-rw-r--r-- | trove/tests/unittests/common/test_exception.py | 28 |
2 files changed, 37 insertions, 1 deletions
diff --git a/trove/common/exception.py b/trove/common/exception.py index 57f92873..25d96466 100644 --- a/trove/common/exception.py +++ b/trove/common/exception.py @@ -16,6 +16,9 @@ # under the License. """I totally stole most of this from melange, thx guys!!!""" + +import re + from trove.openstack.common import log as logging from trove.openstack.common import exception as openstack_exception from trove.openstack.common import processutils @@ -30,6 +33,10 @@ LOG = logging.getLogger(__name__) wrap_exception = openstack_exception.wrap_exception +def safe_fmt_string(text): + return re.sub(r'%([0-9]+)', r'\1', text) + + class TroveError(openstack_exception.OpenstackException): """Base exception that all custom trove app exceptions inherit from.""" internal_message = None @@ -39,9 +46,10 @@ class TroveError(openstack_exception.OpenstackException): self.message = message if self.internal_message is not None: try: - LOG.error(self.internal_message % kwargs) + LOG.error(safe_fmt_string(self.internal_message) % kwargs) except Exception: LOG.error(self.internal_message) + self.message = safe_fmt_string(self.message) super(TroveError, self).__init__(**kwargs) diff --git a/trove/tests/unittests/common/test_exception.py b/trove/tests/unittests/common/test_exception.py new file mode 100644 index 00000000..66752e01 --- /dev/null +++ b/trove/tests/unittests/common/test_exception.py @@ -0,0 +1,28 @@ +# Copyright 2013 OpenStack LLC +# +# 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 testtools import TestCase +from trove.common.exception import TroveError + + +class TroveErrorTest(TestCase): + + def test_valid_error_message_format(self): + error = TroveError("%02d" % 1) + self.assertEqual(error.message, "01") + + def test_invalid_error_message_format(self): + error = TroveError("test%999999sdb") + self.assertEqual(error.message, "test999999sdb") |