diff --git a/src/rougail/error.py b/src/rougail/error.py index 178370c58..1449e23f3 100644 --- a/src/rougail/error.py +++ b/src/rougail/error.py @@ -73,3 +73,15 @@ class DictConsistencyError(Exception): class UpgradeError(Exception): """Error during XML upgrade""" + +## ---- generic exceptions ---- + +class NotFoundError(Exception): + "not found error" + pass + +class VariableNotFoundError(NotFoundError): + "Variable was not found" + def __init__(self, errmsg, varname): + self.varname = varname + diff --git a/src/rougail/object_model.py b/src/rougail/object_model.py index 8e3a02c9c..42816d6a6 100644 --- a/src/rougail/object_model.py +++ b/src/rougail/object_model.py @@ -30,7 +30,7 @@ from pydantic import ( ConfigDict, ) from .utils import get_jinja_variable_to_param, get_realpath - +from .error import VariableNotFoundError BASETYPE = Union[StrictBool, StrictInt, StrictFloat, StrictStr, None] @@ -303,8 +303,9 @@ class VariableCalculation(Calculation): variable_path = self.get_realpath(self.variable) variable, suffix, dynamic = objectspace.paths.get_with_dynamic(variable_path) if not variable: - raise Exception(f"pffff {variable_path}") + raise VariableNotFoundError(f"Variable not found {variable_path}", variable_path) if not isinstance(variable, objectspace.variable): + # FIXME remove the pfff raise Exception("pfff it's a family") param = { "type": "variable", diff --git a/src/rougail/tiramisureflector.py b/src/rougail/tiramisureflector.py index 63717e318..a03de68f3 100644 --- a/src/rougail/tiramisureflector.py +++ b/src/rougail/tiramisureflector.py @@ -33,7 +33,7 @@ from json import dumps from os.path import isfile, basename from .i18n import _ -from .error import DictConsistencyError +from .error import DictConsistencyError, VariableNotFoundError from .utils import normalize_family from .object_model import Calculation, CONVERT_OPTION @@ -575,15 +575,19 @@ class Family(Common): children = [] for path in self.objectspace.parents[self.elt.path]: children.append(self.objectspace.paths[path]) - keys["children"] = ( - "[" - + ", ".join( - [ - self.tiramisu.reflector_objects[child.path].get( - self.calls, self.elt.path - ) - for child in children - ] + try: + keys["children"] = ( + "[" + + ", ".join( + [ + self.tiramisu.reflector_objects[child.path].get( + self.calls, self.elt.path + ) + for child in children + ] + ) + + "]" ) - + "]" - ) + except VariableNotFoundError as exc: + msg = f"The variable '{exc.varname}' is mandatory for the dynamic family" + raise DictConsistencyError(msg, 88, self.elt.xmlfiles)