From 160626b3641efcc5708e50c6168a73fba3ccb806 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Sun, 25 Nov 2012 21:51:59 +0100 Subject: [PATCH] jsonrpc: Give explicit error if calling method without object path --- mopidy/utils/jsonrpc.py | 10 ++++++---- tests/utils/jsonrpc_test.py | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/mopidy/utils/jsonrpc.py b/mopidy/utils/jsonrpc.py index 47b660d3..8230aada 100644 --- a/mopidy/utils/jsonrpc.py +++ b/mopidy/utils/jsonrpc.py @@ -187,10 +187,12 @@ class JsonRpcWrapper(object): # The mounted object contains the callable - if '.' in method_path: - mount, method_name = method_path.rsplit('.', 1) - else: - mount, method_name = '', method_path + if '.' not in method_path: + raise JsonRpcMethodNotFoundError( + data='Could not find object mount in method name "%s"' % ( + method_path)) + + mount, method_name = method_path.rsplit('.', 1) if method_name.startswith('_'): raise JsonRpcMethodNotFoundError( diff --git a/tests/utils/jsonrpc_test.py b/tests/utils/jsonrpc_test.py index d4730be2..64b5e628 100644 --- a/tests/utils/jsonrpc_test.py +++ b/tests/utils/jsonrpc_test.py @@ -387,6 +387,21 @@ class JsonRpcSingleCommandErrorTest(JsonRpcTestBase): self.assertEqual( error['data'], '"params", if given, must be an array or an object') + def test_method_on_without_object_causes_unknown_method_error(self): + request = { + 'jsonrpc': '2.0', + 'method': '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'], + 'Could not find object mount in method name "bogus"') + def test_method_on_unknown_object_causes_unknown_method_error(self): request = { 'jsonrpc': '2.0', @@ -417,7 +432,7 @@ class JsonRpcSingleCommandErrorTest(JsonRpcTestBase): def test_private_method_causes_unknown_method_error(self): request = { 'jsonrpc': '2.0', - 'method': '_secret', + 'method': 'core._secret', 'id': 1, } response = self.jrw.handle_data(request)