From d5a501c59b9f2755823e209a377765a8aa8bc4c2 Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Mon, 26 Dec 2022 19:01:35 +0200 Subject: [PATCH] Handle exceptions reading json file and parsing json data --- restic_exporter/restic_exporter.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/restic_exporter/restic_exporter.py b/restic_exporter/restic_exporter.py index ef551f2..aa09ec4 100644 --- a/restic_exporter/restic_exporter.py +++ b/restic_exporter/restic_exporter.py @@ -60,12 +60,18 @@ class restic_exporter: print(f"{self.metric_name}_{counter}{labels} {float(self.summary[counter])} {self.summary['timestamp']}") def _read_summary_from_json(self, json_file): - with open(json_file, 'r') as file_pointer: - content = file_pointer.readlines() + try: + with open(json_file, 'r') as file_pointer: + content = file_pointer.readlines() + except Exception as error: + self._log.error(f"Error reading file '{json_file}'. Check permissions. {error}") self.summary = {} for line in content: - line_data = json.loads(line) + try: + line_data = json.loads(line) + except json.decoder.JSONDecodeError as error: + self._log.error(f"Error decoding line '{line}'. {error}") if 'message_type' in line_data and line_data['message_type'] == 'summary': self.summary = line_data if 'snapshot_id' in line_data: