Fix i18n when formatting exception
This patch is also adding the missing tests to the format_exception() function since it's related to the fix. Change-Id: Id1ab32076e5d112493216dcd92f80fe6e0199237 Closes-Bug: #1437125
This commit is contained in:
parent
a42d23d8ce
commit
81afe37c0e
@ -2,6 +2,7 @@ import traceback
|
||||
import functools
|
||||
import inspect
|
||||
import logging
|
||||
import six
|
||||
|
||||
import wsme.exc
|
||||
import wsme.types
|
||||
@ -212,15 +213,15 @@ def format_exception(excinfo, debug=False):
|
||||
error = excinfo[1]
|
||||
code = getattr(error, 'code', None)
|
||||
if code and utils.is_valid_code(code) and utils.is_client_error(code):
|
||||
faultstring = error.faultstring if hasattr(error, 'faultstring') \
|
||||
else str(error)
|
||||
faultstring = (error.faultstring if hasattr(error, 'faultstring')
|
||||
else six.text_type(error))
|
||||
r = dict(faultcode="Client",
|
||||
faultstring=faultstring)
|
||||
log.debug("Client-side error: %s" % r['faultstring'])
|
||||
r['debuginfo'] = None
|
||||
return r
|
||||
else:
|
||||
faultstring = str(error)
|
||||
faultstring = six.text_type(error)
|
||||
debuginfo = "\n".join(traceback.format_exception(*excinfo))
|
||||
|
||||
log.error('Server-side error: "%s". Detail: \n%s' % (
|
||||
|
@ -10,8 +10,9 @@ import webtest
|
||||
|
||||
from wsme import WSRoot, expose, validate
|
||||
from wsme.rest import scan_api
|
||||
from wsme.api import FunctionArgument, FunctionDefinition
|
||||
from wsme import types
|
||||
from wsme import exc
|
||||
import wsme.api as wsme_api
|
||||
import wsme.types
|
||||
|
||||
from wsme.tests.test_protocols import DummyProtocol
|
||||
@ -367,8 +368,51 @@ class TestFunctionDefinition(unittest.TestCase):
|
||||
def myfunc(self):
|
||||
pass
|
||||
|
||||
fd = FunctionDefinition(FunctionDefinition)
|
||||
fd.arguments.append(FunctionArgument('a', int, True, None))
|
||||
fd = wsme_api.FunctionDefinition(wsme_api.FunctionDefinition)
|
||||
fd.arguments.append(wsme_api.FunctionArgument('a', int, True, None))
|
||||
|
||||
assert fd.get_arg('a').datatype is int
|
||||
assert fd.get_arg('b') is None
|
||||
|
||||
|
||||
class TestFormatException(unittest.TestCase):
|
||||
|
||||
def _test_format_exception(self, exception, debug=False):
|
||||
fake_exc_info = (None, exception, None)
|
||||
return wsme_api.format_exception(fake_exc_info, debug=debug)
|
||||
|
||||
def test_format_client_exception(self):
|
||||
faultstring = b'boom'
|
||||
ret = self._test_format_exception(exc.ClientSideError(faultstring))
|
||||
self.assertIsNone(ret['debuginfo'])
|
||||
self.assertEqual('Client', ret['faultcode'])
|
||||
self.assertEqual(faultstring, ret['faultstring'])
|
||||
|
||||
def test_format_client_exception_unicode(self):
|
||||
faultstring = u'\xc3\xa3o'
|
||||
ret = self._test_format_exception(exc.ClientSideError(faultstring))
|
||||
self.assertIsNone(ret['debuginfo'])
|
||||
self.assertEqual('Client', ret['faultcode'])
|
||||
self.assertEqual(faultstring, ret['faultstring'])
|
||||
|
||||
def test_format_server_exception(self):
|
||||
faultstring = b'boom'
|
||||
ret = self._test_format_exception(Exception(faultstring))
|
||||
self.assertIsNone(ret['debuginfo'])
|
||||
self.assertEqual('Server', ret['faultcode'])
|
||||
self.assertEqual(faultstring, ret['faultstring'])
|
||||
|
||||
def test_format_server_exception_unicode(self):
|
||||
faultstring = u'\xc3\xa3o'
|
||||
ret = self._test_format_exception(Exception(faultstring))
|
||||
self.assertIsNone(ret['debuginfo'])
|
||||
self.assertEqual('Server', ret['faultcode'])
|
||||
self.assertEqual(faultstring, ret['faultstring'])
|
||||
|
||||
def test_format_server_exception_debug(self):
|
||||
faultstring = b'boom'
|
||||
ret = self._test_format_exception(Exception(faultstring), debug=True)
|
||||
# assert debuginfo is populated
|
||||
self.assertIsNotNone(ret['debuginfo'])
|
||||
self.assertEqual('Server', ret['faultcode'])
|
||||
self.assertEqual(faultstring, ret['faultstring'])
|
||||
|
Loading…
x
Reference in New Issue
Block a user