diff options
author | Julien Danjou <julien@danjou.info> | 2013-10-09 14:47:20 +0200 |
---|---|---|
committer | Julien Danjou <julien@danjou.info> | 2013-10-09 16:08:22 +0200 |
commit | 8a0c4e66c92d8037bb5b101632032dbabfbed809 (patch) | |
tree | b1e6c4199191f290d6d204291927077f86a50015 | |
parent | e3b324e1e45f363d4702d414c04adf7b4f3ed3a9 (diff) | |
download | wsme-8a0c4e66c92d8037bb5b101632032dbabfbed809.tar.gz |
Return a ClientSideError if unable to convert data
This makes thing works at least for Pecan.
Change-Id: I7b02813258d9b8c2eea13e4215a3d711ec2e56ed
-rw-r--r-- | tests/pecantest/test/controllers/ws.py | 4 | ||||
-rw-r--r-- | tests/pecantest/test/tests/test_ws.py | 13 | ||||
-rw-r--r-- | wsme/rest/args.py | 10 |
3 files changed, 25 insertions, 2 deletions
diff --git a/tests/pecantest/test/controllers/ws.py b/tests/pecantest/test/controllers/ws.py index 1dcb0cb..3782d2f 100644 --- a/tests/pecantest/test/controllers/ws.py +++ b/tests/pecantest/test/controllers/ws.py @@ -116,3 +116,7 @@ class AuthorsController(RestController): @wsmeext.pecan.wsexpose(None, int) def delete(self, author_id): print("Deleting", author_id) + + @wsmeext.pecan.wsexpose(Book, int, body=Author) + def put(self, author_id, author=None): + return author diff --git a/tests/pecantest/test/tests/test_ws.py b/tests/pecantest/test/tests/test_ws.py index 4eb9607..53f8dad 100644 --- a/tests/pecantest/test/tests/test_ws.py +++ b/tests/pecantest/test/tests/test_ws.py @@ -69,6 +69,19 @@ class TestWS(FunctionalTest): assert a['id'] == 10 assert a['firstname'] == 'test' + def test_put_parameter_validate(self): + res = self.app.put( + '/authors/foobar', '{"firstname": "test"}', + headers={"Content-Type": "application/json"}, + expect_errors=True + ) + self.assertEqual(res.status_int, 400) + a = json.loads(res.body.decode('utf-8')) + self.assertEqual( + a['faultstring'], + "Invalid input for field/attribute author_id. " + "Value: 'foobar'. unable to convert to int") + def test_clientsideerror(self): expected_status_code = 400 expected_status = http_response_messages[expected_status_code] diff --git a/wsme/rest/args.py b/wsme/rest/args.py index 42ad040..6ab9403 100644 --- a/wsme/rest/args.py +++ b/wsme/rest/args.py @@ -4,7 +4,7 @@ import re from simplegeneric import generic -from wsme.exc import ClientSideError, UnknownArgument +from wsme.exc import ClientSideError, UnknownArgument, InvalidInput from wsme.types import iscomplex, list_attributes, Unset from wsme.types import UserType, ArrayType, DictType, File @@ -168,7 +168,13 @@ def dict_from_params(datatype, params, path, hit_paths): def args_from_args(funcdef, args, kwargs): newargs = [] for argdef, arg in zip(funcdef.arguments[:len(args)], args): - newargs.append(from_param(argdef.datatype, arg)) + try: + newargs.append(from_param(argdef.datatype, arg)) + except Exception: + raise InvalidInput( + argdef.name, + arg, + "unable to convert to %s" % argdef.datatype.__name__) newkwargs = {} for argname, value in kwargs.items(): newkwargs[argname] = from_param( |