Skip to content

Commit dd40b99

Browse files
author
btgoodwin
committed
Added try-catch to trap errors and reflect them back using the exception handler (vs. just dumping errors at the server console
1 parent d95b46d commit dd40b99

10 files changed

Lines changed: 347 additions & 250 deletions

File tree

rest/allocation.py

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,52 +35,51 @@
3535
class Allocations(JsonHandler, PropertyHelper):
3636
@gen.coroutine
3737
def get(self, domain_name, allocation_id=None):
38-
39-
if allocation_id:
40-
allocation = yield self.redhawk.get_allocation(domain_name, allocation_id)
41-
42-
info = {
43-
'id': allocation.allocationID,
44-
'deviceId': allocation.allocatedDevice._get_identifier(),
45-
'deviceManagerId': allocation.allocationDeviceManager._get_identifier(),
46-
'properties': self.format_properties(allocation.allocationProperties),
47-
'sourceId': allocation.sourceID
48-
}
49-
else:
50-
allocations = yield self.redhawk.get_allocation_list(domain_name)
51-
52-
info = {'allocations': allocations}
53-
54-
self._render_json(info)
38+
try:
39+
if allocation_id:
40+
allocation = yield self.redhawk.get_allocation(domain_name, allocation_id)
41+
42+
info = {
43+
'id': allocation.allocationID,
44+
'deviceId': allocation.allocatedDevice._get_identifier(),
45+
'deviceManagerId': allocation.allocationDeviceManager._get_identifier(),
46+
'properties': self.format_properties(allocation.allocationProperties),
47+
'sourceId': allocation.sourceID
48+
}
49+
else:
50+
allocations = yield self.redhawk.get_allocation_list(domain_name)
51+
52+
info = {'allocations': allocations}
53+
54+
self._render_json(info)
55+
except Exception as e:
56+
self._handle_request_exception(e)
5557

5658
@gen.coroutine
57-
def post(self, domain_name, allocation_id=None):
58-
data = json.loads(self.request.body)
59-
json_device_ids = data.get('deviceIds', [])
60-
json_props = data.get('properties', [])
61-
json_source_id = data.get('sourceId', '')
62-
props = self.unformat_properties_without_query(json_props)
63-
64-
if not allocation_id:
65-
allocation_id = data.get('allocationId', '')
59+
def post(self, domain_name, *args):
60+
try:
61+
data = json.loads(self.request.body)
62+
json_device_ids = data.get('deviceIds', [])
63+
json_props = data.get('properties', [])
64+
json_source_id = data.get('sourceId', '')
65+
props = self.unformat_properties_without_query(json_props)
6666

67-
allocation_id = yield self.redhawk.allocate(domain_name, allocation_id, json_device_ids, props, json_source_id)
68-
allocations = yield self.redhawk.get_allocation_list(domain_name)
67+
if 'allocationId' in data:
68+
allocation_id = data['allocationId']
69+
else:
70+
raise Exception('ALLOCATION_ID is required for allocations.')
6971

70-
self._render_json({'allocated': allocation_id, 'allocations': allocations})
72+
allocation_id = yield self.redhawk.allocate(domain_name, allocation_id, json_device_ids, props, json_source_id)
73+
allocations = yield self.redhawk.get_allocation_list(domain_name)
7174

72-
@gen.coroutine
73-
def delete(self, domain_name, allocation_id=None):
75+
self._render_json({'allocated': allocation_id, 'allocations': allocations})
7476

75-
if allocation_id:
76-
yield self.redhawk.deallocate(domain_name, [allocation_id])
77-
allocations = yield self.redhawk.get_allocation_list(domain_name)
77+
except Exception as e:
78+
self._handle_request_exception(e)
7879

79-
info = {
80-
'deallocated': [allocation_id],
81-
'allocations': allocations
82-
}
83-
else:
80+
@gen.coroutine
81+
def delete(self, domain_name, *args):
82+
try:
8483
data = json.loads(self.request.body)
8584
allocation_ids = data.get('allocationIds', [])
8685

@@ -92,4 +91,7 @@ def delete(self, domain_name, allocation_id=None):
9291
'allocations': allocations
9392
}
9493

95-
self._render_json(info)
94+
self._render_json(info)
95+
96+
except Exception as e:
97+
self._handle_request_exception(e)

rest/application.py

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,70 +35,83 @@
3535
class Applications(JsonHandler, PropertyHelper, PortHelper):
3636
@gen.coroutine
3737
def get(self, domain_name, app_id=None):
38+
try:
39+
if app_id:
40+
app = yield self.redhawk.get_application(domain_name, app_id)
41+
comps = yield self.redhawk.get_component_list(domain_name, app_id)
42+
43+
info = {
44+
'id': app._get_identifier(),
45+
'name': app.name,
46+
'started': app._get_started(),
47+
'components': comps,
48+
'ports': self.format_ports(app.ports),
49+
'properties': self.format_properties(app._externalProps, app.query([]))
50+
}
51+
else:
52+
apps = yield self.redhawk.get_application_list(domain_name)
53+
wfs = yield self.redhawk.get_available_applications(domain_name)
3854

39-
if app_id:
40-
app = yield self.redhawk.get_application(domain_name, app_id)
41-
comps = yield self.redhawk.get_component_list(domain_name, app_id)
42-
43-
info = {
44-
'id': app._get_identifier(),
45-
'name': app.name,
46-
'started': app._get_started(),
47-
'components': comps,
48-
'ports': self.format_ports(app.ports),
49-
'properties': self.format_properties(app._externalProps, app.query([]))
50-
}
51-
else:
52-
apps = yield self.redhawk.get_application_list(domain_name)
53-
wfs = yield self.redhawk.get_available_applications(domain_name)
54-
55-
info = {'applications': apps, 'waveforms': wfs}
55+
info = {'applications': apps, 'waveforms': wfs}
5656

57-
self._render_json(info)
57+
self._render_json(info)
58+
except Exception as e:
59+
self._handle_request_exception(e)
5860

5961
@gen.coroutine
6062
def post(self, domain_name, app_id=None):
61-
data = json.loads(self.request.body)
63+
try:
64+
data = json.loads(self.request.body)
6265

63-
if app_id:
64-
app = yield self.redhawk.get_application(domain_name, app_id)
66+
if app_id:
67+
app = yield self.redhawk.get_application(domain_name, app_id)
6568

66-
started = data['started']
67-
if started:
68-
app.start()
69+
started = data['started']
70+
if started:
71+
app.start()
72+
else:
73+
app.stop()
74+
75+
self._render_json({'id': app_id, 'started': app._get_started()})
6976
else:
70-
app.stop()
77+
app_name = str(data['name'])
7178

72-
self._render_json({'id': app_id, 'started': app._get_started()})
73-
else:
74-
app_name = str(data['name'])
79+
app_id = yield self.redhawk.launch_application(domain_name, app_name)
80+
apps = yield self.redhawk.get_application_list(domain_name)
7581

76-
app_id = yield self.redhawk.launch_application(domain_name, app_name)
77-
apps = yield self.redhawk.get_application_list(domain_name)
82+
if 'started' in data and data['started']:
83+
app = yield self.redhawk.get_application(domain_name, app_id)
84+
app.start()
7885

79-
if 'started' in data and data['started']:
80-
app = yield self.redhawk.get_application(domain_name, app_id)
81-
app.start()
86+
self._render_json({'launched': app_id, 'applications': apps})
8287

83-
self._render_json({'launched': app_id, 'applications': apps})
88+
except Exception as e:
89+
self._handle_request_exception(e)
8490

8591
@gen.coroutine
8692
def put(self, domain_name, app_id=None):
87-
data = json.loads(self.request.body)
93+
try:
94+
data = json.loads(self.request.body)
8895

89-
app = yield self.redhawk.get_application(domain_name, app_id)
96+
app = yield self.redhawk.get_application(domain_name, app_id)
9097

91-
started = data['started']
92-
if started:
93-
app.start()
94-
else:
95-
app.stop()
98+
started = data['started']
99+
if started:
100+
app.start()
101+
else:
102+
app.stop()
96103

97-
self._render_json({'id': app_id, 'started': app._get_started()})
104+
self._render_json({'id': app_id, 'started': app._get_started()})
105+
except Exception as e:
106+
self._handle_request_exception(e)
98107

99108
@gen.coroutine
100109
def delete(self, domain_name, app_id):
101-
yield self.redhawk.release_application(domain_name, app_id)
102-
apps = yield self.redhawk.get_application_list(domain_name)
110+
try:
111+
yield self.redhawk.release_application(domain_name, app_id)
112+
apps = yield self.redhawk.get_application_list(domain_name)
103113

104-
self._render_json({'released': app_id, 'applications': apps})
114+
self._render_json({'released': app_id, 'applications': apps})
115+
116+
except Exception as e:
117+
self._handle_request_exception(e)

rest/component.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,45 @@
3535
class Components(JsonHandler, PropertyHelper, PortHelper):
3636
@gen.coroutine
3737
def get(self, domain_name, app_id, comp_id=None):
38-
if comp_id:
39-
comp = yield self.redhawk.get_component(domain_name, app_id, comp_id)
38+
try:
39+
if comp_id:
40+
comp = yield self.redhawk.get_component(domain_name, app_id, comp_id)
4041

41-
info = {
42-
'name': comp.name,
43-
'id': comp._id,
44-
'started': comp._get_started(),
45-
'ports': self.format_ports(comp.ports),
46-
'properties': self.format_properties(comp._properties, comp.query([]))
47-
}
48-
else:
49-
comps = yield self.redhawk.get_component_list(domain_name, app_id)
50-
info = {'components': comps}
42+
info = {
43+
'name': comp.name,
44+
'id': comp._id,
45+
'started': comp._get_started(),
46+
'ports': self.format_ports(comp.ports),
47+
'properties': self.format_properties(comp._properties, comp.query([]))
48+
}
49+
else:
50+
comps = yield self.redhawk.get_component_list(domain_name, app_id)
51+
info = {'components': comps}
5152

52-
self._render_json(info)
53+
self._render_json(info)
54+
except Exception as e:
55+
self._handle_request_exception(e)
5356

5457

5558
class ComponentProperties(JsonHandler, PropertyHelper):
5659
@gen.coroutine
5760
def get(self, domain, application, component):
58-
comp = yield self.redhawk.get_component(domain, application, component)
61+
try:
62+
comp = yield self.redhawk.get_component(domain, application, component)
5963

60-
self._render_json({
61-
'properties': self.format_properties(comp._properties, comp.query([]))
62-
})
64+
self._render_json({
65+
'properties': self.format_properties(comp._properties, comp.query([]))
66+
})
67+
except Exception as e:
68+
self._handle_request_exception(e)
6369

6470
@gen.coroutine
6571
def put(self, domain, application, component):
66-
data = json.loads(self.request.body)
67-
json_props = data.get('properties', [])
68-
changes = self.unformat_properties(json_props)
72+
try:
73+
data = json.loads(self.request.body)
74+
json_props = data.get('properties', [])
75+
changes = self.unformat_properties(json_props)
6976

70-
yield self.redhawk.component_configure(domain, application, component, changes)
77+
yield self.redhawk.component_configure(domain, application, component, changes)
78+
except Exception as e:
79+
self._handle_request_exception(e)

rest/device.py

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,51 +34,60 @@
3434
class Devices(JsonHandler, PropertyHelper, PortHelper):
3535
@gen.coroutine
3636
def get(self, domainName, managerId, deviceId=None):
37-
if deviceId:
38-
dev = yield self.redhawk.get_device(domainName, managerId, deviceId)
37+
try:
38+
if deviceId:
39+
dev = yield self.redhawk.get_device(domainName, managerId, deviceId)
3940

40-
info = {
41-
'name': dev.name,
42-
'id': dev._id,
43-
'started': dev._get_started(),
44-
'ports': self.format_ports(dev.ports),
45-
'properties': self.format_properties(dev._properties, dev.query([]))
46-
}
47-
else:
48-
devices = yield self.redhawk.get_device_list(domainName, managerId)
49-
info = {'devices': devices}
41+
info = {
42+
'name': dev.name,
43+
'id': dev._id,
44+
'started': dev._get_started(),
45+
'ports': self.format_ports(dev.ports),
46+
'properties': self.format_properties(dev._properties, dev.query([]))
47+
}
48+
else:
49+
devices = yield self.redhawk.get_device_list(domainName, managerId)
50+
info = {'devices': devices}
5051

51-
self._render_json(info)
52+
self._render_json(info)
53+
except Exception as e:
54+
self._handle_request_exception(e)
5255

5356

5457
class DeviceProperties(JsonHandler, PropertyHelper):
5558
@gen.coroutine
5659
def get(self, domainName, managerId, deviceId):
57-
dev = yield self.redhawk.get_device(domainName, managerId, deviceId)
60+
try:
61+
dev = yield self.redhawk.get_device(domainName, managerId, deviceId)
5862

59-
self._render_json({
60-
'properties': self.format_properties(dev._properties, dev.query([]))
61-
})
63+
self._render_json({
64+
'properties': self.format_properties(dev._properties, dev.query([]))
65+
})
66+
except Exception as e:
67+
self._handle_request_exception(e)
6268

6369
@gen.coroutine
6470
def put(self, domainName, managerId, deviceId):
65-
PUT_METHODS = {
66-
'configure' : self.redhawk.device_configure,
67-
'allocate' : self.redhawk.device_allocate,
68-
'deallocate' : self.redhawk.device_deallocate
69-
}
70-
data = json.loads(self.request.body)
71-
json_props = data.get('properties', [])
72-
changes = self.unformat_properties(json_props)
73-
74-
cb = PUT_METHODS.get(data['method'], None)
75-
try:
76-
r, message = yield cb(domainName, managerId, deviceId, changes)
77-
if 'configure' == data['method']:
78-
self._render_json({ 'method': data['method'], 'status': True , 'message': message})
79-
else:
80-
self._render_json({ 'method': data['method'], 'status': r , 'message': message})
71+
try:
72+
PUT_METHODS = {
73+
'configure' : self.redhawk.device_configure,
74+
'allocate' : self.redhawk.device_allocate,
75+
'deallocate' : self.redhawk.device_deallocate
76+
}
77+
data = json.loads(self.request.body)
78+
json_props = data.get('properties', [])
79+
changes = self.unformat_properties(json_props)
80+
81+
cb = PUT_METHODS.get(data['method'], None)
82+
try:
83+
r, message = yield cb(domainName, managerId, deviceId, changes)
84+
if 'configure' == data['method']:
85+
self._render_json({ 'method': data['method'], 'status': True , 'message': message})
86+
else:
87+
self._render_json({ 'method': data['method'], 'status': r , 'message': message})
8188

89+
except Exception as e:
90+
self._render_json({ 'method': data['method'], 'status': False, 'message': "{0}".format(e) })
8291
except Exception as e:
83-
self._render_json({ 'method': data['method'], 'status': False, 'message': "{0}".format(e) })
92+
self._handle_request_exception(e)
8493

0 commit comments

Comments
 (0)