Skip to content

Commit e3282de

Browse files
authored
Merge pull request #10 from internap/exception
Allow remote module to transmit exception to Ubersmith
2 parents aadca8b + d72f73c commit e3282de

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

tests/test_api.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import unittest
2020

2121
from ubersmith_remote_module_server.api import Api
22+
from ubersmith_remote_module_server.exceptions import RemoteModuleException
2223

2324

2425
class ApiTest(unittest.TestCase):
@@ -66,6 +67,26 @@ def test_execute_method_returns_string(self):
6667

6768
self.router.invoke_method.assert_called_with(module=self.module2, method='remote_method', params=[], env={'variable1': 'value1'}, callback={})
6869
assert_that(json.loads(output.data.decode(output.charset)), is_('simple string'))
70+
assert_that(output.status_code, is_(200))
71+
72+
def test_execute_method_raise_an_exception(self):
73+
self.router.invoke_method.side_effect = RemoteModuleException('Some Error')
74+
output = self.api_client.post(self.generate_module_path('module2'),
75+
headers={'Content-Type': 'application/json'},
76+
data=json.dumps(
77+
{
78+
"method": "remote_method",
79+
"params": [],
80+
"env": {
81+
"variable1": "value1"
82+
},
83+
"callback": {}
84+
}
85+
))
86+
87+
self.router.invoke_method.assert_called_with(module=self.module2, method='remote_method', params=[], env={'variable1': 'value1'}, callback={})
88+
assert_that(json.loads(output.data.decode(output.charset)), is_('Some Error'))
89+
assert_that(output.status_code, is_(500))
6990

7091
def test_execute_method_returns_list(self):
7192
self.router.invoke_method.return_value = ['a', 'b', 'c']
@@ -84,6 +105,7 @@ def test_execute_method_returns_list(self):
84105

85106
self.router.invoke_method.assert_called_with(module=self.module2, method='remote_method', params=[], env={'variable1': 'value1'}, callback={})
86107
assert_that(json.loads(output.data.decode(output.charset)), is_(['a', 'b', 'c']))
108+
assert_that(output.status_code, is_(200))
87109

88110
def test_invoking_unknown_module_returns_a_404(self):
89111
output = self.api_client.post(self.generate_module_path('new_module'),
@@ -106,6 +128,7 @@ def test_listing_unknown_module_returns_a_404(self):
106128

107129
assert_that(output.status_code, is_(404))
108130

131+
109132
class NoTrailingSlashApiTest(ApiTest):
110133
def generate_module_path(self, module_name):
111134
return '/{0}'.format(module_name)

ubersmith_remote_module_server/api.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import logging
1818

1919
from flask import request, current_app
20+
from ubersmith_remote_module_server.exceptions import RemoteModuleException
2021

2122

2223
class Api(object):
@@ -41,8 +42,13 @@ def list_implemented_methods(self, module):
4142
def handle_remote_invocation(self, module):
4243
logging.debug("Handle remote invocation for {module}".format(module=module))
4344
data = request.get_json()
44-
output = self.router.invoke_method(module=module, **data)
45-
return json_response(output, 200)
45+
try:
46+
output = self.router.invoke_method(module=module, **data)
47+
return json_response(output, 200)
48+
except RemoteModuleException as e:
49+
logging.exception(e)
50+
return json_response(str(e), 500)
51+
4652

4753
def json_response(data, code):
4854
json_data = json.dumps(data, indent=None)

ubersmith_remote_module_server/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ class NamedArgumentsOnly(Exception):
2323
def __init__(self, msg="UbersmithCore was called with non-named arguments, "
2424
"you MUST use named arguments (kwargs)"):
2525
super(NamedArgumentsOnly, self).__init__(msg)
26+
27+
28+
class RemoteModuleException(Exception):
29+
pass

0 commit comments

Comments
 (0)