Merge "Allow fault code customization"
This commit is contained in:
commit
b261be6e73
@ -217,7 +217,8 @@ def format_exception(excinfo, debug=False):
|
|||||||
if code and utils.is_valid_code(code) and utils.is_client_error(code):
|
if code and utils.is_valid_code(code) and utils.is_client_error(code):
|
||||||
faultstring = (error.faultstring if hasattr(error, 'faultstring')
|
faultstring = (error.faultstring if hasattr(error, 'faultstring')
|
||||||
else six.text_type(error))
|
else six.text_type(error))
|
||||||
r = dict(faultcode="Client",
|
faultcode = getattr(error, 'faultcode', 'Client')
|
||||||
|
r = dict(faultcode=faultcode,
|
||||||
faultstring=faultstring)
|
faultstring=faultstring)
|
||||||
log.debug("Client-side error: %s" % r['faultstring'])
|
log.debug("Client-side error: %s" % r['faultstring'])
|
||||||
r['debuginfo'] = None
|
r['debuginfo'] = None
|
||||||
@ -229,7 +230,8 @@ def format_exception(excinfo, debug=False):
|
|||||||
log.error('Server-side error: "%s". Detail: \n%s' % (
|
log.error('Server-side error: "%s". Detail: \n%s' % (
|
||||||
faultstring, debuginfo))
|
faultstring, debuginfo))
|
||||||
|
|
||||||
r = dict(faultcode="Server", faultstring=faultstring)
|
faultcode = getattr(error, 'faultcode', 'Server')
|
||||||
|
r = dict(faultcode=faultcode, faultstring=faultstring)
|
||||||
if debug:
|
if debug:
|
||||||
r['debuginfo'] = debuginfo
|
r['debuginfo'] = debuginfo
|
||||||
else:
|
else:
|
||||||
|
@ -4,9 +4,10 @@ from wsme.utils import _
|
|||||||
|
|
||||||
|
|
||||||
class ClientSideError(RuntimeError):
|
class ClientSideError(RuntimeError):
|
||||||
def __init__(self, msg=None, status_code=400):
|
def __init__(self, msg=None, status_code=400, faultcode='Client'):
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
self.code = status_code
|
self.code = status_code
|
||||||
|
self.faultcode = faultcode
|
||||||
super(ClientSideError, self).__init__(self.faultstring)
|
super(ClientSideError, self).__init__(self.faultstring)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -396,6 +396,15 @@ class TestFormatException(unittest.TestCase):
|
|||||||
self.assertEqual('Client', ret['faultcode'])
|
self.assertEqual('Client', ret['faultcode'])
|
||||||
self.assertEqual(faultstring, ret['faultstring'])
|
self.assertEqual(faultstring, ret['faultstring'])
|
||||||
|
|
||||||
|
def test_format_client_exception_with_faultcode(self):
|
||||||
|
faultcode = 'AccessDenied'
|
||||||
|
faultstring = 'boom'
|
||||||
|
ret = self._test_format_exception(
|
||||||
|
exc.ClientSideError(faultstring, faultcode=faultcode))
|
||||||
|
self.assertIsNone(ret['debuginfo'])
|
||||||
|
self.assertEqual('AccessDenied', ret['faultcode'])
|
||||||
|
self.assertEqual(faultstring, ret['faultstring'])
|
||||||
|
|
||||||
def test_format_server_exception(self):
|
def test_format_server_exception(self):
|
||||||
faultstring = 'boom'
|
faultstring = 'boom'
|
||||||
ret = self._test_format_exception(Exception(faultstring))
|
ret = self._test_format_exception(Exception(faultstring))
|
||||||
@ -410,6 +419,15 @@ class TestFormatException(unittest.TestCase):
|
|||||||
self.assertEqual('Server', ret['faultcode'])
|
self.assertEqual('Server', ret['faultcode'])
|
||||||
self.assertEqual(faultstring, ret['faultstring'])
|
self.assertEqual(faultstring, ret['faultstring'])
|
||||||
|
|
||||||
|
def test_format_server_exception_with_faultcode(self):
|
||||||
|
faultstring = 'boom'
|
||||||
|
exception = Exception(faultstring)
|
||||||
|
exception.faultcode = 'ServerError'
|
||||||
|
ret = self._test_format_exception(exception)
|
||||||
|
self.assertIsNone(ret['debuginfo'])
|
||||||
|
self.assertEqual('ServerError', ret['faultcode'])
|
||||||
|
self.assertEqual(faultstring, ret['faultstring'])
|
||||||
|
|
||||||
def test_format_server_exception_debug(self):
|
def test_format_server_exception_debug(self):
|
||||||
faultstring = 'boom'
|
faultstring = 'boom'
|
||||||
ret = self._test_format_exception(Exception(faultstring), debug=True)
|
ret = self._test_format_exception(Exception(faultstring), debug=True)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user