diff --git a/piperider_cli/event/collector.py b/piperider_cli/event/collector.py index 39f51167f..8a2619a95 100644 --- a/piperider_cli/event/collector.py +++ b/piperider_cli/event/collector.py @@ -4,6 +4,7 @@ import sys import time from datetime import datetime +from json import JSONDecodeError import portalocker import requests @@ -64,7 +65,6 @@ def log_event(self, prop, event_type): app_version=__version__, ) - # TODO: handle exception when writing to file self._store_to_file(event) if self._is_full(): self.send_events() @@ -102,7 +102,10 @@ def send_events(self): def _store_to_file(self, event): with portalocker.Lock(self._unsend_events_file, 'r+', timeout=5) as f: - o = json.loads(f.read()) + try: + o = json.loads(f.read()) + except JSONDecodeError: + o = dict(unsend_events=[]) events = o.get('unsend_events', None) if events is None: o['unsend_events'] = []