Skip to content

Commit 8d4f9d8

Browse files
committed
adds ctx
1 parent 52771a9 commit 8d4f9d8

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

packages/models-library/src/models_library/errors_classes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ def _get_full_class_name(cls) -> str:
3030
)
3131
]
3232
return ".".join(reversed(relevant_classes))
33+
34+
def ctx(self):
35+
"""Returns context stored in the exception"""
36+
return dict(**self.__dict__)

packages/models-library/tests/test_errors_classes.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,14 @@ class MyErrorAfter(OsparcErrorMixin, ValueError):
148148
msg_template = "{value} and {missing}"
149149

150150
assert str(MyErrorAfter(value=42)) == "42 and 'missing=?'"
151+
152+
153+
def test_exception_context():
154+
class MyError(OsparcErrorMixin, ValueError):
155+
msg_template = "{value} and {missing}"
156+
157+
exc = MyError(value=42, missing="foo", extra="bar")
158+
assert exc.ctx() == {"value": 42, "missing": "foo", "extra": "bar"}
159+
160+
exc = MyError(value=42)
161+
assert exc.ctx() == {"value": 42}

0 commit comments

Comments
 (0)