Skip to content

Commit 72dd7e7

Browse files
authored
IndexReader exception handling incase the index files are broken (aws#264)
1 parent 9ea721c commit 72dd7e7

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

tornasole/core/index_reader.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from tornasole.core.logger import get_logger
88
from tornasole.core.tfrecord.tensor_reader import TensorReader
99
from tornasole.core.modes import ModeKeys
10+
from tornasole.exceptions import IndexReaderException
1011

1112

1213
"""
@@ -121,9 +122,17 @@ def load_tensor_data_from_index_files(path, start_after_key=None, range_steps=No
121122
tensor_data = IndexReader._update_tensors_from_json(tensor_data, step, response, path, worker)
122123
return tensor_data, last_index_token
123124

125+
@staticmethod
126+
def _validate(index_dict):
127+
if 'meta' not in index_dict or len(index_dict['meta']) == 0:
128+
raise IndexReaderException('meta section is not present')
129+
if 'tensor_payload' not in index_dict:
130+
raise IndexReaderException('tensor_payload section is not present')
131+
124132
@staticmethod
125133
def _update_tensors_from_json(index_tensors_dict, step, response, path, worker):
126134
index_dict = json.loads(response)
135+
IndexReader._validate(index_dict)
127136
index_meta = index_dict['meta']
128137
mode = index_meta['mode']
129138
mode = ModeKeys[mode.strip()]

tornasole/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ def __str__(self):
1111
self.mode.name)
1212

1313

14+
class IndexReaderException(Exception):
15+
def __init__(self, message):
16+
self.message = message
17+
18+
def __str__(self):
19+
return self.message
20+
21+
1422
class StepUnavailable(Exception):
1523
def __init__(self, step, mode):
1624
self.step = step

0 commit comments

Comments
 (0)