Skip to content

Commit

Permalink
fix(notepad): improve error handling and validation in remove_value f…
Browse files Browse the repository at this point in the history
…unction

- Enhanced the `remove_value` function to validate the `position` parameter, ensuring it is an integer and within the bounds of the notepad DataFrame.
- Added specific `ValueError` messages for invalid position inputs, improving user feedback.
- Updated error handling in the `NotepadComponent` to provide clearer context in exception messages when operations fail.

These changes enhance the robustness and usability of the notepad operations, ensuring better error management and user experience.
  • Loading branch information
ogabrielluiz committed Jan 13, 2025
1 parent d072df6 commit b627646
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/backend/base/langflow/components/helpers/notepad.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,27 @@ def remove_value(notepad: DataFrame, value: str, position: int | None = None) ->
Returns:
DataFrame: A new DataFrame with the specified value removed
Raises:
ValueError: If position is not a valid integer or is out of bounds
"""
notepad_length = notepad.shape[0]
if position is not None and 0 <= position < notepad_length:
# Remove at specific position

# If position is provided, validate it's within bounds
if position is not None:
if not isinstance(position, int):
msg = f"Position must be an integer, got {type(position)}"
raise ValueError(msg)
if position < 0 or position >= notepad_length:
msg = f"Position {position} is out of bounds for notepad of length {notepad_length}"
raise ValueError(msg)
# Remove at valid position
return notepad.drop(notepad.index[position]).reset_index(drop=True)

# Remove by value if it exists
if notepad_length > 0 and len(notepad[notepad["value"] == value]) > 0:
# Remove by value
return notepad[notepad["value"] != value].reset_index(drop=True)

return notepad


Expand Down Expand Up @@ -214,7 +227,7 @@ async def process_and_get_notepad(self) -> DataFrame:
try:
new_df = operation_func(notepad, value, position)
except Exception as exc:
msg = f"Error performing operation: {operation}"
msg = f"Error performing operation {operation} on notepad: {exc}"
raise ValueError(msg) from exc

if not isinstance(new_df, DataFrame):
Expand Down

0 comments on commit b627646

Please sign in to comment.