From 2c816d5a3831e2ce81834cab290027c994fa9586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 7 Mar 2018 14:45:43 +0100 Subject: [PATCH] Implement error handling on EventHandler exposing APIs --- lib/tfw/components/process_managing_event_handler.py | 9 ++++++--- lib/tfw/components/source_code_event_handler.py | 11 +++++++---- lib/tfw/components/terminado_event_handler.py | 7 +++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/tfw/components/process_managing_event_handler.py b/lib/tfw/components/process_managing_event_handler.py index 6fcc248..ab63607 100644 --- a/lib/tfw/components/process_managing_event_handler.py +++ b/lib/tfw/components/process_managing_event_handler.py @@ -36,6 +36,9 @@ class ProcessManagingEventHandler(TriggerlessEventHandler): @_with_monitor_paused def handle_event(self, key, data_json): - data = data_json['data'] - self.processmanager(data['command'], data['process_name']) - self.uplink.send(self.key, {'data': {'process_name': data['process_name']}}) + try: + data = data_json['data'] + self.processmanager(data['command'], data['process_name']) + self.uplink.send(self.key, {'data': {'process_name': data['process_name']}}) + except KeyError: + log.error('IGNORING MESSAGE: Invalid data_json received: {}'.format(data_json)) diff --git a/lib/tfw/components/source_code_event_handler.py b/lib/tfw/components/source_code_event_handler.py index dd26072..003264a 100644 --- a/lib/tfw/components/source_code_event_handler.py +++ b/lib/tfw/components/source_code_event_handler.py @@ -84,10 +84,13 @@ class SourceCodeEventHandler(TriggerlessEventHandler): data['files'] = self.filemanager.files def handle_event(self, key, data_json): - data = data_json['data'] - data_json['data'] = self.commands[data['command']](data) - self.attach_fileinfo(data) - return data_json + try: + data = data_json['data'] + data_json['data'] = self.commands[data['command']](data) + self.attach_fileinfo(data) + return data_json + except KeyError: + log.error('IGNORING MESSAGE: Invalid data_json received: {}'.format(data_json)) def cleanup(self): self.monitor.stop() diff --git a/lib/tfw/components/terminado_event_handler.py b/lib/tfw/components/terminado_event_handler.py index 4a8f98a..649f823 100644 --- a/lib/tfw/components/terminado_event_handler.py +++ b/lib/tfw/components/terminado_event_handler.py @@ -23,8 +23,11 @@ class TerminadoEventHandler(TriggerlessEventHandler): def handle_event(self, key, data_json): log.debug('TerminadoEventHandler received event: {}'.format(data_json)) - data_json['data'] = self.commands[data_json['data']['command']](data_json['data']) - return data_json + try: + data_json['data'] = self.commands[data_json['data']['command']](data_json['data']) + return data_json + except KeyError: + log.error('IGNORING MESSAGE: Invalid data_json received: {}'.format(data_json)) def write(self, data): self.terminado_server.pty.write(data['shellcmd'])