Skip to content

Commit 19a293c

Browse files
author
Frédéric Guillot
committed
Allow remote module to transmit exception to Ubersmith
1 parent aadca8b commit 19a293c

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

tests/test_api.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ def test_execute_method_returns_string(self):
6666

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

7090
def test_execute_method_returns_list(self):
7191
self.router.invoke_method.return_value = ['a', 'b', 'c']
@@ -84,6 +104,7 @@ def test_execute_method_returns_list(self):
84104

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

88109
def test_invoking_unknown_module_returns_a_404(self):
89110
output = self.api_client.post(self.generate_module_path('new_module'),
@@ -106,6 +127,7 @@ def test_listing_unknown_module_returns_a_404(self):
106127

107128
assert_that(output.status_code, is_(404))
108129

130+
109131
class NoTrailingSlashApiTest(ApiTest):
110132
def generate_module_path(self, module_name):
111133
return '/{0}'.format(module_name)

ubersmith_remote_module_server/api.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ def list_implemented_methods(self, module):
4141
def handle_remote_invocation(self, module):
4242
logging.debug("Handle remote invocation for {module}".format(module=module))
4343
data = request.get_json()
44-
output = self.router.invoke_method(module=module, **data)
45-
return json_response(output, 200)
44+
try:
45+
output = self.router.invoke_method(module=module, **data)
46+
return json_response(output, 200)
47+
except Exception as e:
48+
return json_response(str(e), 500)
49+
4650

4751
def json_response(data, code):
4852
json_data = json.dumps(data, indent=None)

0 commit comments

Comments
 (0)