From e926338b69c08605587a0993ff5242be521ab580 Mon Sep 17 00:00:00 2001 From: Pim Nelissen Date: Wed, 25 Feb 2026 14:17:14 +0100 Subject: [PATCH] add InvalidIsotopeError and DimensionError --- src/pg_rad/exceptions/exceptions.py | 14 +++++++++++--- src/pg_rad/isotopes/isotope.py | 3 ++- src/pg_rad/objects/objects.py | 4 +++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/pg_rad/exceptions/exceptions.py b/src/pg_rad/exceptions/exceptions.py index 37355aa..0b5a474 100644 --- a/src/pg_rad/exceptions/exceptions.py +++ b/src/pg_rad/exceptions/exceptions.py @@ -14,12 +14,20 @@ class OutOfBoundsError(Exception): """Raised when an object is attempted to be placed out of bounds.""" -class MissingNestedKeyError(Exception): - """Raised when a nested key is missing in the config.""" +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}: {subkey}" + self.message = f"Missing key in {key}: {', '.join(list(subkey))}" else: self.message = f"Missing key: {key}" super().__init__(self.message) + + +class DimensionError(ValueError): + """Raised if dimensions or coordinates do not match the system.""" + + +class InvalidIsotopeError(ValueError): + """Raised if attempting to load an isotope that is not valid.""" diff --git a/src/pg_rad/isotopes/isotope.py b/src/pg_rad/isotopes/isotope.py index 218b36f..9e7aa77 100644 --- a/src/pg_rad/isotopes/isotope.py +++ b/src/pg_rad/isotopes/isotope.py @@ -1,5 +1,6 @@ from typing import Dict, Type +from pg_rad.exceptions.exceptions import InvalidIsotopeError from pg_rad.physics.attenuation import get_mass_attenuation_coeff @@ -46,5 +47,5 @@ preset_isotopes: Dict[str, Type[Isotope]] = { def get_isotope(isotope_str: str) -> Isotope: """Lazy factory function to create isotope objects.""" if isotope_str not in preset_isotopes: - raise ValueError(f"Unknown isotope: {isotope_str}") + raise InvalidIsotopeError(f"Unknown isotope: {isotope_str}") return preset_isotopes[isotope_str]() diff --git a/src/pg_rad/objects/objects.py b/src/pg_rad/objects/objects.py index d3ec285..ab036cd 100644 --- a/src/pg_rad/objects/objects.py +++ b/src/pg_rad/objects/objects.py @@ -2,6 +2,8 @@ from typing import Self import numpy as np +from pg_rad.exceptions.exceptions import DimensionError + class BaseObject: def __init__( @@ -21,7 +23,7 @@ class BaseObject: """ if len(pos) != 3: - raise ValueError("Position must be tuple of length 3 (x,y,z).") + raise DimensionError("Position must be tuple of length 3 (x,y,z).") self.pos = pos self.name = name self.color = color