diff --git a/tests/test_api.py b/tests/test_api.py index fc5ac42..0e3dde9 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -19,6 +19,7 @@ import unittest from ubersmith_remote_module_server.api import Api +from ubersmith_remote_module_server.exceptions import RemoteModuleException class ApiTest(unittest.TestCase): @@ -66,6 +67,26 @@ def test_execute_method_returns_string(self): self.router.invoke_method.assert_called_with(module=self.module2, method='remote_method', params=[], env={'variable1': 'value1'}, callback={}) assert_that(json.loads(output.data.decode(output.charset)), is_('simple string')) + assert_that(output.status_code, is_(200)) + + def test_execute_method_raise_an_exception(self): + self.router.invoke_method.side_effect = RemoteModuleException('Some Error') + output = self.api_client.post(self.generate_module_path('module2'), + headers={'Content-Type': 'application/json'}, + data=json.dumps( + { + "method": "remote_method", + "params": [], + "env": { + "variable1": "value1" + }, + "callback": {} + } + )) + + self.router.invoke_method.assert_called_with(module=self.module2, method='remote_method', params=[], env={'variable1': 'value1'}, callback={}) + assert_that(json.loads(output.data.decode(output.charset)), is_('Some Error')) + assert_that(output.status_code, is_(500)) def test_execute_method_returns_list(self): self.router.invoke_method.return_value = ['a', 'b', 'c'] @@ -84,6 +105,7 @@ def test_execute_method_returns_list(self): self.router.invoke_method.assert_called_with(module=self.module2, method='remote_method', params=[], env={'variable1': 'value1'}, callback={}) assert_that(json.loads(output.data.decode(output.charset)), is_(['a', 'b', 'c'])) + assert_that(output.status_code, is_(200)) def test_invoking_unknown_module_returns_a_404(self): output = self.api_client.post(self.generate_module_path('new_module'), @@ -106,6 +128,7 @@ def test_listing_unknown_module_returns_a_404(self): assert_that(output.status_code, is_(404)) + class NoTrailingSlashApiTest(ApiTest): def generate_module_path(self, module_name): return '/{0}'.format(module_name) \ No newline at end of file diff --git a/ubersmith_remote_module_server/api.py b/ubersmith_remote_module_server/api.py index 4c42e55..89baf88 100644 --- a/ubersmith_remote_module_server/api.py +++ b/ubersmith_remote_module_server/api.py @@ -17,6 +17,7 @@ import logging from flask import request, current_app +from ubersmith_remote_module_server.exceptions import RemoteModuleException class Api(object): @@ -41,8 +42,13 @@ def list_implemented_methods(self, module): def handle_remote_invocation(self, module): logging.debug("Handle remote invocation for {module}".format(module=module)) data = request.get_json() - output = self.router.invoke_method(module=module, **data) - return json_response(output, 200) + try: + output = self.router.invoke_method(module=module, **data) + return json_response(output, 200) + except RemoteModuleException as e: + logging.exception(e) + return json_response(str(e), 500) + def json_response(data, code): json_data = json.dumps(data, indent=None) diff --git a/ubersmith_remote_module_server/exceptions.py b/ubersmith_remote_module_server/exceptions.py index 27b5549..b65773d 100644 --- a/ubersmith_remote_module_server/exceptions.py +++ b/ubersmith_remote_module_server/exceptions.py @@ -23,3 +23,7 @@ class NamedArgumentsOnly(Exception): def __init__(self, msg="UbersmithCore was called with non-named arguments, " "you MUST use named arguments (kwargs)"): super(NamedArgumentsOnly, self).__init__(msg) + + +class RemoteModuleException(Exception): + pass