Skip to content

Commit e48b5b6

Browse files
committed
Fix: improve error handling in AddMetadataParam value conversion
1 parent 2712a9a commit e48b5b6

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

datalab/gui/panel/base.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,28 +1208,34 @@ def get_conversion_choices(self, _item=None, _value=None):
12081208
def build_values(
12091209
self, objs: list[TypeObj] | None = None
12101210
) -> list[str | float | int | bool]:
1211-
"""Build values according to current parameters."""
1211+
"""Build values according to current parameters.
1212+
1213+
Raises:
1214+
ValueError: If a value cannot be converted to the target type.
1215+
"""
12121216
objs = objs or self.__objs
12131217
# Generate values using the pattern
12141218
raw_values = format_basenames(objs, self.value_pattern)
12151219

12161220
# Convert values according to the selected conversion type
12171221
converted_values = []
1218-
for value_str in raw_values:
1222+
for i, value_str in enumerate(raw_values, start=1):
12191223
if self.conversion == "string":
12201224
converted_values.append(value_str)
12211225
elif self.conversion == "float":
12221226
try:
12231227
converted_values.append(float(value_str))
1224-
except ValueError:
1225-
# Keep as string if conversion fails
1226-
converted_values.append(value_str)
1228+
except ValueError as exc:
1229+
raise ValueError(
1230+
f"Cannot convert value at index {i} to float: '{value_str}'"
1231+
) from exc
12271232
elif self.conversion == "int":
12281233
try:
12291234
converted_values.append(int(value_str))
1230-
except ValueError:
1231-
# Keep as string if conversion fails
1232-
converted_values.append(value_str)
1235+
except ValueError as exc:
1236+
raise ValueError(
1237+
f"Cannot convert value at index {i} to integer: '{value_str}'"
1238+
) from exc
12331239
elif self.conversion == "bool":
12341240
# Convert to boolean: "true", "1", "yes" -> True, others -> False
12351241
lower_val = value_str.lower()
@@ -1251,8 +1257,11 @@ def update_preview(self, _item=None, _value=None) -> None:
12511257
obj_id = str(i)
12521258
preview_lines.append(f"{obj_id}: {self.metadata_key} = {value!r}")
12531259
self.preview = "\n".join(preview_lines)
1254-
except (ValueError, KeyError, TypeError) as exc:
1255-
# Handle formatting errors gracefully (e.g., incomplete format string)
1260+
except ValueError as exc:
1261+
# Handle conversion errors
1262+
self.preview = f"Invalid conversion:{os.linesep}{exc}"
1263+
except (KeyError, TypeError) as exc:
1264+
# Handle formatting errors (e.g., incomplete format string)
12561265
self.preview = f"Invalid pattern:{os.linesep}{exc}"
12571266

12581267
metadata_key = gds.StringItem(

0 commit comments

Comments
 (0)