From 938b3a7afcb548d5f7cf254662c927eaf72ca8cc Mon Sep 17 00:00:00 2001 From: Pim Nelissen Date: Tue, 10 Mar 2026 20:44:45 +0100 Subject: [PATCH] update exceptions and main.py (including test case) --- src/pg_rad/exceptions/exceptions.py | 10 +++++++++- src/pg_rad/main.py | 21 ++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/pg_rad/exceptions/exceptions.py b/src/pg_rad/exceptions/exceptions.py index aac816d..ae7a72e 100644 --- a/src/pg_rad/exceptions/exceptions.py +++ b/src/pg_rad/exceptions/exceptions.py @@ -10,6 +10,10 @@ class InvalidCSVError(DataLoadError): """Raised when a file is not a valid CSV.""" +class InvalidYAMLError(DataLoadError): + """Raised when a file is not a valid YAML.""" + + class OutOfBoundsError(Exception): """Raised when an object is attempted to be placed out of bounds.""" @@ -18,7 +22,11 @@ class MissingConfigKeyError(KeyError): """Raised when a (nested) config key is missing in the config.""" def __init__(self, key, subkey=None): if subkey: - self.message = f"Missing key in {key}: {', '.join(list(subkey))}" + if isinstance(subkey, str): + pass + elif isinstance(subkey, set): + subkey = ', '.join(list(subkey)) + self.message = f"Missing key in {key}: {subkey}" else: self.message = f"Missing key: {key}" diff --git a/src/pg_rad/main.py b/src/pg_rad/main.py index e944fb5..bfc741c 100644 --- a/src/pg_rad/main.py +++ b/src/pg_rad/main.py @@ -3,7 +3,6 @@ import logging import sys from pandas.errors import ParserError -from yaml import YAMLError from pg_rad.detector.builder import DetectorBuilder from pg_rad.exceptions.exceptions import ( @@ -11,7 +10,8 @@ from pg_rad.exceptions.exceptions import ( OutOfBoundsError, DimensionError, InvalidConfigValueError, - InvalidIsotopeError + InvalidIsotopeError, + InvalidYAMLError ) from pg_rad.logger.logger import setup_logger from pg_rad.inputparser.parser import ConfigParser @@ -59,12 +59,14 @@ def main(): length: 1000 segments: - straight + - turn_left + direction: negative sources: test_source: - activity_MBq: 1000 - position: [500, 100, 0] - isotope: CS137 + activity_MBq: 100 + position: [250, 100, 0] + isotope: Cs137 detector: name: dummy @@ -100,8 +102,7 @@ def main(): plotter.plot() except ( MissingConfigKeyError, - KeyError, - YAMLError, + KeyError ) as e: logger.critical(e) logger.critical( @@ -125,8 +126,10 @@ def main(): except ( FileNotFoundError, - ParserError - ): + ParserError, + InvalidYAMLError + ) as e: + logger.critical(e) sys.exit(1)