Handle exceptions reading json file and parsing json data

This commit is contained in:
Antonio J. Delgado 2022-12-26 19:01:35 +02:00
parent a94e755b96
commit d5a501c59b

View file

@ -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: