From 1670793bfed19d5d3dbc5683abb2726bca43d1f7 Mon Sep 17 00:00:00 2001 From: Vladyslav Drok Date: Thu, 22 Dec 2016 14:32:01 +0200 Subject: [PATCH] Print exceptions raised from from_param methods This makes debugging the problems easier. Change-Id: I285d8176b8cfb0cdf1e9cecd8ae5a0a7f0f109e7 --- tests/pecantest/test/tests/test_ws.py | 3 ++- wsme/rest/args.py | 5 +++-- wsme/tests/test_protocols_commons.py | 25 +++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/tests/pecantest/test/tests/test_ws.py b/tests/pecantest/test/tests/test_ws.py index 2d1d242..ae06650 100644 --- a/tests/pecantest/test/tests/test_ws.py +++ b/tests/pecantest/test/tests/test_ws.py @@ -91,7 +91,8 @@ class TestWS(FunctionalTest): self.assertEqual( a['faultstring'], "Invalid input for field/attribute author_id. " - "Value: 'foobar'. unable to convert to int") + "Value: 'foobar'. unable to convert to int. Error: invalid " + "literal for int() with base 10: 'foobar'") def test_clientsideerror(self): expected_status_code = 400 diff --git a/wsme/rest/args.py b/wsme/rest/args.py index 5268e86..91557fc 100644 --- a/wsme/rest/args.py +++ b/wsme/rest/args.py @@ -178,7 +178,7 @@ def args_from_args(funcdef, args, kwargs): for argdef, arg in zip(funcdef.arguments[:len(args)], args): try: newargs.append(from_param(argdef.datatype, arg)) - except Exception: + except Exception as e: if isinstance(argdef.datatype, UserType): datatype_name = argdef.datatype.name elif isinstance(argdef.datatype, type): @@ -188,7 +188,8 @@ def args_from_args(funcdef, args, kwargs): raise InvalidInput( argdef.name, arg, - "unable to convert to %s" % datatype_name) + "unable to convert to %(datatype)s. Error: %(error)s" % { + 'datatype': datatype_name, 'error': e}) newkwargs = {} for argname, value in kwargs.items(): newkwargs[argname] = from_param( diff --git a/wsme/tests/test_protocols_commons.py b/wsme/tests/test_protocols_commons.py index 6d99c45..af5aa05 100644 --- a/wsme/tests/test_protocols_commons.py +++ b/wsme/tests/test_protocols_commons.py @@ -93,6 +93,31 @@ class TestProtocolsCommons(unittest.TestCase): else: self.fail('Should have thrown an InvalidInput') + def test_args_from_args_custom_exc(self): + + class FakeType(UserType): + name = 'fake-type' + basetype = int + + def validate(self, value): + if value < 10: + raise ValueError('should be greater than 10') + + def frombasetype(self, value): + self.validate(value) + + fake_type = FakeType() + fd = FunctionDefinition(FunctionDefinition) + fd.arguments.append(FunctionArgument('fake-arg', fake_type, True, 0)) + + try: + args_from_args(fd, [9], {}) + except InvalidInput as e: + assert fake_type.name in str(e) + assert 'Error: should be greater than 10' in str(e) + else: + self.fail('Should have thrown an InvalidInput') + def test_args_from_args_array_type(self): fake_type = ArrayType(MyBaseType) fd = FunctionDefinition(FunctionDefinition)