Skip to content

Commit 9c992fa

Browse files
committed
feat(derived parameters): gracefully catch errors when deriving parameters inside tests
The propeller size function has not inverse, hence we canot infer the propeller size specification from the FC parameter values. The test file tests/acceptance_template_import_from_params.py assumes size 0, and that causes parameter derivation to fail. This code catches that.
1 parent a8689e5 commit 9c992fa

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

ardupilot_methodic_configurator/backend_filesystem_configuration_steps.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def compute_parameters( # pylint: disable=too-many-branches, too-many-arguments
175175
if parameter_type + "_parameters" not in file_info or not variables:
176176
return ""
177177
destination = self.forced_parameters if parameter_type == "forced" else self.derived_parameters
178-
for parameter, parameter_info in file_info[parameter_type + "_parameters"].items(): # pylint: disable=too-many-nested-blocks
178+
for parameter, parameter_info in file_info[parameter_type + "_parameters"].items():
179179
try:
180180
if ("fc_parameters" in str(parameter_info["New Value"])) and (
181181
"fc_parameters" not in variables or variables["fc_parameters"] == {}
@@ -191,7 +191,24 @@ def compute_parameters( # pylint: disable=too-many-branches, too-many-arguments
191191
if not ignore_fc_derived_param_warnings:
192192
logging_warning(error_msg)
193193
continue
194-
result = eval(str(parameter_info["New Value"]), {}, variables) # noqa: S307 pylint: disable=eval-used
194+
195+
try:
196+
result = eval(str(parameter_info["New Value"]), {}, variables) # noqa: S307 pylint: disable=eval-used
197+
except (ZeroDivisionError, ValueError):
198+
# Handle math errors like:
199+
# - ZeroDivisionError: division by zero or 0.0 raised to negative power
200+
# - ValueError: math domain error (e.g., Diameter_inches**-0.838 when Diameter_inches is 0)
201+
error_msg = _(
202+
"In file '{self.configuration_steps_filename}': '{filename}' {parameter_type} "
203+
"parameter '{parameter}' evaluation resulted in math error: {math_error}"
204+
)
205+
error_msg = error_msg.format(**locals())
206+
if parameter_type == "forced":
207+
logging_error(error_msg)
208+
return error_msg
209+
if not ignore_fc_derived_param_warnings:
210+
logging_warning(error_msg)
211+
continue
195212

196213
# convert (combobox) string text to (parameter value) string int or float
197214
if isinstance(result, str):

0 commit comments

Comments
 (0)