From 50814d3929b15c7077ea423e34ee7b0c4c31f8fc Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Sun, 25 Nov 2012 21:23:55 +0100 Subject: [PATCH] jsonrpc: Explain why a method wasn't found --- mopidy/utils/jsonrpc.py | 14 ++++++++++++-- tests/utils/jsonrpc_test.py | 20 +++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/mopidy/utils/jsonrpc.py b/mopidy/utils/jsonrpc.py index 4bcf3a1c..9aed38cd 100644 --- a/mopidy/utils/jsonrpc.py +++ b/mopidy/utils/jsonrpc.py @@ -202,18 +202,28 @@ class JsonRpcWrapper(object): return self.objects[method_path] # The mounted object contains the callable + if '.' in method_path: mount, method_name = method_path.rsplit('.', 1) else: mount, method_name = '', method_path + if method_name.startswith('_'): raise JsonRpcMethodNotFoundError( data='Private methods are not exported') + try: obj = self.objects[mount] + except KeyError: + raise JsonRpcMethodNotFoundError( + data='No object found at "%s"' % mount) + + try: return getattr(obj, method_name) - except (AttributeError, KeyError): - raise JsonRpcMethodNotFoundError() + except AttributeError: + raise JsonRpcMethodNotFoundError( + data='Object mounted at "%s" has no member "%s"' % ( + mount, method_name)) def _is_notification(self, request): return 'id' not in request diff --git a/tests/utils/jsonrpc_test.py b/tests/utils/jsonrpc_test.py index fa9d2b4c..8c487e87 100644 --- a/tests/utils/jsonrpc_test.py +++ b/tests/utils/jsonrpc_test.py @@ -381,11 +381,10 @@ class JsonRpcSingleCommandErrorTest(JsonRpcTestBase): self.assertEqual( error['data'], '"params", if given, must be an array or an object') - def test_unknown_method_causes_unknown_method_error(self): + def test_method_on_unknown_object_causes_unknown_method_error(self): request = { 'jsonrpc': '2.0', - 'method': 'bogus', - 'params': ['bogus'], + 'method': 'bogus.bogus', 'id': 1, } response = self.jrw.handle_data(request) @@ -393,6 +392,21 @@ class JsonRpcSingleCommandErrorTest(JsonRpcTestBase): error = response['error'] self.assertEqual(error['code'], -32601) self.assertEqual(error['message'], 'Method not found') + self.assertEqual(error['data'], 'No object found at "bogus"') + + def test_unknown_method_on_known_object_causes_unknown_method_error(self): + request = { + 'jsonrpc': '2.0', + 'method': 'core.bogus', + 'id': 1, + } + response = self.jrw.handle_data(request) + + error = response['error'] + self.assertEqual(error['code'], -32601) + self.assertEqual(error['message'], 'Method not found') + self.assertEqual( + error['data'], 'Object mounted at "core" has no member "bogus"') def test_private_method_causes_unknown_method_error(self): request = {