Skip to content

Commit 461789b

Browse files
committed
extra try...except to protect the config parser
1 parent b0d49be commit 461789b

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/core/app.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
class Args:
3636
verbose: bool # verbose logging
3737
cbid: bool # force color by id
38-
cfg: str | None # config path
38+
cfg: str # config path
3939
frac: float | None # fraction of points to render
4040
voxel_size: float | None # voxel size for downsampling
4141
downsample: bool # downsample based on either voxel size or fraction
@@ -278,9 +278,24 @@ def __setup(self) -> None:
278278
'Failed to parse json config file : '
279279
'maximum nesting level could be reached, please check your file\n%s', e)
280280
sys.exit(1)
281-
default: dict[str, Any] = raw_data['default']
282-
configs: list[dict[str, Any]] = raw_data['configs']
283-
cfgs = [Config.from_json(json=cfg, **default) for cfg in configs]
281+
if not raw_data:
282+
self.log.critical('Failed to parse %s : empty file', self.args.cfg)
283+
sys.exit(1)
284+
default: dict[str, Any] = None
285+
configs: list[dict[str, Any]] = None
286+
try:
287+
default = raw_data['default']
288+
configs = raw_data['configs']
289+
except KeyError as e:
290+
self.log.critical('Failed to parse %s : %s', self.args.cfg, e)
291+
sys.exit(1)
292+
cfgs: list[Config] = []
293+
try:
294+
for cfg in configs:
295+
cfgs.append(Config.from_json(json=cfg, **default))
296+
except ValueError as e:
297+
self.log.critical('Failed to parse config n°%d : %s', len(cfgs), e)
298+
sys.exit(1)
284299

285300
fset: list[int] = None
286301
if self.args.only and any(map(lambda x: x > len(cfgs), self.args.only)): # pylint: disable=bad-builtin

0 commit comments

Comments
 (0)