feat: raises a convenient error for a dynamic family

When a dynicamic family references to an unexistent multi variable,
an error is raised.
This commit is contained in:
gwen 2024-03-20 12:08:24 +01:00
parent 897264b779
commit d2244c7bc5
3 changed files with 31 additions and 14 deletions

View file

@ -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

View file

@ -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",

View file

@ -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,6 +575,7 @@ class Family(Common):
children = []
for path in self.objectspace.parents[self.elt.path]:
children.append(self.objectspace.paths[path])
try:
keys["children"] = (
"["
+ ", ".join(
@ -587,3 +588,6 @@ class Family(Common):
)
+ "]"
)
except VariableNotFoundError as exc:
msg = f"The variable '{exc.varname}' is mandatory for the dynamic family"
raise DictConsistencyError(msg, 88, self.elt.xmlfiles)