Skip to content

Commit 41c8aff

Browse files
committed
fix: crashing on no grains found
1 parent 6338694 commit 41c8aff

1 file changed

Lines changed: 16 additions & 36 deletions

File tree

src/napari_topostats/_widget_function.py

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,17 @@ def evaluate_path_to_data(path_to_data, return_value, instance=None, type_class=
133133
except KeyError as e:
134134
raised_error = e
135135
if raised_error:
136-
return construct_error_args(
137-
message=f"Couldn't find data for either direction in path: {path_to_data}",
138-
raise_exception=True,
139-
topostats_error=True,
140-
exception=raised_error,
141-
)
136+
raise ValueError("Couldn't find data for either direction") from raised_error
142137
if path_to_data.startswith("return"):
143138
return _eval(return_value, path_to_data[6:]) if len(path_to_data) > 6 else return_value
144139

145140
if path_to_data.startswith("obj"):
146141
if type_class:
147142
return _eval(instance, path_to_data[3:]) if len(path_to_data) > 3 else instance
148143
else:
149-
return construct_error_args(
150-
message=f"Invalid path_to_data: {path_to_data} - 'obj' requires type_class",
151-
)
144+
raise ValueError(f"Invalid path_to_data: {path_to_data} - 'obj' requires type_class")
152145

153-
return construct_error_args(message=f"Invalid path_to_data: {path_to_data}", topostats_error=True)
146+
raise ValueError(f"Invalid path_to_data: {path_to_data}")
154147

155148

156149
# Class representation of each function in the button grid.
@@ -501,36 +494,21 @@ def func_to_execute(**kwargs):
501494
# Execute function or method
502495
# pylint: disable=too-many-return-statements
503496
def _func():
504-
if self.type_class:
505-
# ruff: noqa: BLE001
506-
try:
497+
# ruff: noqa: BLE001
498+
try:
499+
if self.type_class:
507500
instance = self.type_class(**class_args)
508-
except Exception as e:
509-
return construct_error_args(
510-
e, raise_exception=True, topostats_error=True, type_class=self.type_class
511-
)
512-
method = getattr(instance, self.function_to_run.__name__, None)
513-
if method:
514-
# ruff: noqa: BLE001
515-
try:
501+
method = getattr(instance, self.function_to_run.__name__, None)
502+
if method:
516503
return_value = method(**method_args)
517-
except Exception as e:
504+
else:
518505
return construct_error_args(
519-
e, raise_exception=True, topostats_error=True, type_class=self.type_class
506+
message=f"Method {self.function_to_run.__name__} not found on instance."
520507
)
521508
else:
522-
return construct_error_args(
523-
message=f"Method {self.function_to_run.__name__} not found on instance."
524-
)
525-
else:
526-
# ruff: noqa: BLE001
527-
try:
528509
return_value = func_to_execute(**method_args)
529-
except Exception as e:
530-
return construct_error_args(e, raise_exception=True, topostats_error=True)
531-
# Evaluate path_to_data
532-
metadata = {}
533-
try:
510+
# Evaluate path_to_data
511+
metadata = {}
534512
if self.metadata_paths is not None:
535513
for key in self.metadata_paths:
536514
if self.metadata_paths[key] == "config":
@@ -539,7 +517,7 @@ def _func():
539517
metadata[key] = evaluate_path_to_data(
540518
self.metadata_paths[key],
541519
return_value,
542-
instance,
520+
instance if self.type_class else None,
543521
self.type_class,
544522
)
545523
if self.type_class and hasattr(instance, "topostats_object"):
@@ -555,7 +533,9 @@ def _func():
555533
else:
556534
result = evaluate_path_to_data(self.path_to_data, return_value)
557535
except Exception as e:
558-
return construct_error_args(exception=e, raise_exception=True, topostats_error=True)
536+
return construct_error_args(
537+
exception=e, raise_exception=True, topostats_error=True, type_class=self.type_class
538+
)
559539
if isinstance(result, dict) and "message" in result and "exception" in result:
560540
return result
561541
return (result, metadata)

0 commit comments

Comments
 (0)