Skip to content

Commit

Permalink
Merge pull request #10 from internap/exception
Browse files Browse the repository at this point in the history
Allow remote module to transmit exception to Ubersmith
  • Loading branch information
Marx314 committed Mar 14, 2017
2 parents aadca8b + d72f73c commit e3282de
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
23 changes: 23 additions & 0 deletions tests/test_api.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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']
Expand All @@ -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'),
Expand All @@ -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)
10 changes: 8 additions & 2 deletions ubersmith_remote_module_server/api.py
Expand Up @@ -17,6 +17,7 @@
import logging

from flask import request, current_app
from ubersmith_remote_module_server.exceptions import RemoteModuleException


class Api(object):
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions ubersmith_remote_module_server/exceptions.py
Expand Up @@ -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

0 comments on commit e3282de

Please sign in to comment.