From 408c46d1a2e389a600f12b665d514421d04006f1 Mon Sep 17 00:00:00 2001 From: Matthew Wildoer Date: Wed, 4 Sep 2024 15:55:10 -0700 Subject: [PATCH] Core: Improve field construction exceptions --- src/faebryk/core/node.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/faebryk/core/node.py b/src/faebryk/core/node.py index 637a9829..f39d3234 100644 --- a/src/faebryk/core/node.py +++ b/src/faebryk/core/node.py @@ -42,6 +42,8 @@ logger = logging.getLogger(__name__) +# TODO: should this be a FaebrykException? +# TODO: should this include node and field information? class FieldError(Exception): pass @@ -110,6 +112,13 @@ def __init__(self, node: "Node", *args: object) -> None: self.node = node +class FieldConstructionError(FaebrykException): + def __init__(self, node: "Node", field: str, *args: object) -> None: + super().__init__(*args) + self.node = node + self.field = field + + class NodeAlreadyBound(NodeException): def __init__(self, node: "Node", other: "Node", *args: object) -> None: super().__init__( @@ -291,7 +300,7 @@ def append(name, inst): return inst - def setup_field(name, obj): + def _setup_field(name, obj): def setup_gen_alias(name, obj): origin = get_origin(obj) assert origin @@ -321,6 +330,16 @@ def setup_gen_alias(name, obj): raise NotImplementedError() + def setup_field(name, obj): + try: + _setup_field(name, obj) + except Exception as e: + raise FieldConstructionError( + self, + name, + f'An exception occurred while constructing field "{name}"', + ) from e + nonrt, rt = partition(lambda x: isinstance(x[1], rt_field), clsfields.items()) for name, obj in nonrt: setup_field(name, obj)