diff --git a/wsme/rest/args.py b/wsme/rest/args.py index 1492222..4574cdd 100644 --- a/wsme/rest/args.py +++ b/wsme/rest/args.py @@ -171,10 +171,14 @@ def args_from_args(funcdef, args, kwargs): try: newargs.append(from_param(argdef.datatype, arg)) except Exception: + if isinstance(argdef.datatype, UserType): + datatype_name = argdef.datatype.name + else: + datatype_name = argdef.datatype.__name__ raise InvalidInput( argdef.name, arg, - "unable to convert to %s" % argdef.datatype.__name__) + "unable to convert to %s" % datatype_name) 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 b496de6..19908b2 100644 --- a/wsme/tests/test_protocols_commons.py +++ b/wsme/tests/test_protocols_commons.py @@ -3,7 +3,9 @@ import datetime import unittest -from wsme.rest.args import from_param, from_params +from wsme.api import FunctionArgument, FunctionDefinition +from wsme.rest.args import from_param, from_params, args_from_args +from wsme.exc import InvalidInput from wsme.types import UserType, Unset, ArrayType, DictType @@ -52,3 +54,24 @@ class TestProtocolsCommons(unittest.TestCase): def test_from_params_dict_unset(self): assert from_params(DictType(int, str), {}, 'a', set()) is Unset + + def test_args_from_args_usertype(self): + + class FakeType(UserType): + name = 'fake-type' + basetype = int + + fake_type = FakeType() + fd = FunctionDefinition(FunctionDefinition) + fd.arguments.append(FunctionArgument('fake-arg', fake_type, True, 0)) + + new_args = args_from_args(fd, [1], {}) + self.assertEqual([1], new_args[0]) + + # can't convert str to int + try: + args_from_args(fd, ['invalid-argument'], {}) + except InvalidInput as e: + assert fake_type.name in str(e) + else: + self.fail('Should have thrown an InvalidInput')