jsonrpc: Explain why a method wasn't found

This commit is contained in:
Stein Magnus Jodal 2012-11-25 21:23:55 +01:00
parent 609fdc46ca
commit 50814d3929
2 changed files with 29 additions and 5 deletions

View File

@ -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

View File

@ -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 = {