diff --git a/ironic/common/exception.py b/ironic/common/exception.py index 71f0bd86e2..6b62c380a8 100644 --- a/ironic/common/exception.py +++ b/ironic/common/exception.py @@ -88,6 +88,13 @@ class IronicException(Exception): super(IronicException, self).__init__(message) + def __str__(self): + """Encode to utf-8 then wsme api can consume it as well.""" + if not six.PY3: + return unicode(self.args[0]).encode('utf-8') + + return self.args[0] + def format_message(self): if self.__class__.__name__.endswith('_Remote'): return self.args[0] diff --git a/ironic/tests/test_exception.py b/ironic/tests/test_exception.py new file mode 100644 index 0000000000..75e8c7cfb1 --- /dev/null +++ b/ironic/tests/test_exception.py @@ -0,0 +1,29 @@ +# Copyright (c) 2015 IBM, Corp. +# +# 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. + +import six + +from ironic.common import exception +from ironic.tests import base + + +class TestIronicException(base.TestCase): + def test____init__(self): + expected = '\xc3\xa9\xe0\xaf\xb2\xe0\xbe\x84' + if six.PY3: + message = chr(233) + chr(0x0bf2) + chr(3972) + else: + message = unichr(233) + unichr(0x0bf2) + unichr(3972) + exc = exception.IronicException(message) + self.assertEqual(expected, exc.__str__())