Skip to content

Commit 42381b6

Browse files
authored
refactor: remove custom exceptions, instead raise ValueError (#1558)
* refactor: remove custom exceptions, instead raise ValueError * chore: lint
1 parent 88d9a43 commit 42381b6

File tree

2 files changed

+26
-47
lines changed

2 files changed

+26
-47
lines changed

src/aind_data_schema/utils/validators.py

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,6 @@ class TimeValidation(Enum):
2323
"""Time should be before the end time."""
2424

2525

26-
class CoordinateSystemException(Exception):
27-
"""Raised when a coordinate system is missing."""
28-
29-
def __init__(self):
30-
"""Initialize the exception."""
31-
super().__init__("CoordinateSystem is required when a Transform or Coordinate is present")
32-
33-
34-
class SystemNameException(Exception):
35-
"""Raised when there is a system name mismatch."""
36-
37-
def __init__(self, expected, found):
38-
"""Initialize the exception with the expected and found axis counts."""
39-
self.expected = expected
40-
self.found = found
41-
super().__init__(f"System name mismatch, expected {expected}, found {found}")
42-
43-
44-
class AxisCountException(Exception):
45-
"""Raised when the axis count does not match."""
46-
47-
def __init__(self, expected, found):
48-
"""Initialize the exception with the expected and found axis counts."""
49-
self.expected = expected
50-
self.found = found
51-
super().__init__(f"Axis count mismatch, expected {expected} axes, but found {found}")
52-
53-
5426
def subject_specimen_id_compatibility(subject_id: str, specimen_id: str) -> bool:
5527
"""Check whether a subject_id and specimen_id are compatible"""
5628
return subject_id in specimen_id
@@ -149,13 +121,20 @@ def _recurse_helper(data, **kwargs):
149121
recursive_coord_system_check(attr_value, **kwargs)
150122

151123

152-
def _system_check_helper(data, coordinate_system_name: str, axis_count: int):
124+
def _system_check_helper(data, coordinate_system_name: Optional[str], axis_count: Optional[int]):
153125
"""Helper function to raise errors if the coordinate_system_name or axis_count don't match"""
126+
object_type = getattr(data, "object_type", type(data).__name__)
127+
154128
if not coordinate_system_name or not axis_count:
155-
raise CoordinateSystemException()
129+
raise ValueError(
130+
f"CoordinateSystem is required when a Transform or Coordinate is present (object_type: {object_type})"
131+
)
156132

157133
if data.coordinate_system_name not in coordinate_system_name:
158-
raise SystemNameException(coordinate_system_name, data.coordinate_system_name)
134+
raise ValueError(
135+
f"System name mismatch for {object_type}, expected {coordinate_system_name}, "
136+
f"found {data.coordinate_system_name}"
137+
)
159138

160139
# Check lengths of subfields based on class types
161140
if hasattr(data, "__dict__"):
@@ -169,10 +148,13 @@ def _system_check_helper(data, coordinate_system_name: str, axis_count: int):
169148
if sub_data and hasattr(sub_data, field_name):
170149
field_value = getattr(sub_data, field_name)
171150
if len(field_value) != axis_count:
172-
raise AxisCountException(axis_count, len(field_value))
151+
raise ValueError(
152+
f"Axis count mismatch for {object_type}, expected {axis_count} axes, "
153+
f"but found {len(field_value)}"
154+
)
173155

174156

175-
def recursive_coord_system_check(data, coordinate_system_name: str, axis_count: int):
157+
def recursive_coord_system_check(data, coordinate_system_name: Optional[str], axis_count: Optional[int]):
176158
"""Recursively check fields, see if they are Coordinates and check if they match a List[values]
177159
178160
Note that we just need to check if the axes all show up, not necessarily in matching order

tests/test_utils_validators.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
from aind_data_schema.components.coordinates import Rotation, Scale, Translation
1414
from aind_data_schema.components.wrappers import AssetPath
1515
from aind_data_schema.utils.validators import (
16-
AxisCountException,
17-
CoordinateSystemException,
18-
SystemNameException,
1916
TimeValidation,
2017
_convert_to_comparable,
2118
_recurse_helper,
@@ -107,27 +104,27 @@ def test_system_check_helper_valid(self):
107104

108105
def test_system_check_helper_missing_system_name(self):
109106
"""Test _system_check_helper with missing coordinate_system_name"""
110-
with self.assertRaises(CoordinateSystemException):
107+
with self.assertRaises(ValueError):
111108
_system_check_helper(self.translation_wrapper, None, axis_count=2)
112109

113110
def test_system_check_helper_missing_axis_count(self):
114111
"""Test _system_check_helper with missing axis_count"""
115-
with self.assertRaises(CoordinateSystemException):
112+
with self.assertRaises(ValueError):
116113
_system_check_helper(self.translation_wrapper, self.coordinate_system_name, axis_count=None)
117114

118115
def test_system_check_helper_wrong_system_name(self):
119116
"""Test _system_check_helper with wrong coordinate_system_name"""
120-
with self.assertRaises(SystemNameException) as context:
117+
with self.assertRaises(ValueError) as context:
121118
_system_check_helper(self.translation_wrapper, "WRONG_SYSTEM", axis_count=2)
122-
self.assertEqual("WRONG_SYSTEM", context.exception.expected)
123-
self.assertEqual(self.coordinate_system_name, context.exception.found)
119+
self.assertIn("WRONG_SYSTEM", str(context.exception))
120+
self.assertIn(self.coordinate_system_name, str(context.exception))
124121

125122
def test_system_check_helper_wrong_axis_count(self):
126123
"""Test _system_check_helper with wrong axis_count"""
127-
with self.assertRaises(AxisCountException) as context:
124+
with self.assertRaises(ValueError) as context:
128125
_system_check_helper(self.translation_wrapper, self.coordinate_system_name, axis_count=3)
129-
self.assertEqual(3, context.exception.expected)
130-
self.assertEqual(2, context.exception.found)
126+
self.assertIn("3", str(context.exception))
127+
self.assertIn("2", str(context.exception))
131128

132129
def test_system_check_helper_multiple_axis_types(self):
133130
"""Test _system_check_helper with multiple axis types"""
@@ -176,7 +173,7 @@ def test_recursive_coord_system_check_with_invalid_system_name(self):
176173
translation=[0.5, 1],
177174
),
178175
)
179-
with self.assertRaises(SystemNameException) as context:
176+
with self.assertRaises(ValueError) as context:
180177
recursive_coord_system_check(data, self.coordinate_system_name, axis_count=2)
181178

182179
self.assertIn("System name mismatch", str(context.exception))
@@ -212,7 +209,7 @@ def test_recursive_coord_system_check_with_axis_count_mismatch(self):
212209
translation=[0.5, 1, 2],
213210
),
214211
)
215-
with self.assertRaises(AxisCountException) as context:
212+
with self.assertRaises(ValueError) as context:
216213
recursive_coord_system_check(data, self.coordinate_system_name, axis_count=2)
217214

218215
self.assertIn("Axis count mismatch", str(context.exception))
@@ -227,7 +224,7 @@ class MockData(BaseModel):
227224

228225
data = MockData(coordinate_system_name=self.coordinate_system_name)
229226

230-
with self.assertRaises(CoordinateSystemException) as context:
227+
with self.assertRaises(ValueError) as context:
231228
recursive_coord_system_check(data, None, axis_count=0)
232229

233230
self.assertIn("CoordinateSystem is required", str(context.exception))

0 commit comments

Comments
 (0)