Skip to content

Commit 738e13d

Browse files
author
feryc@hotmail.com
committed
fix: narrow except in search_directory to OSError/UnicodeDecodeError only
1 parent 6193bd4 commit 738e13d

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ def search_directory(
938938
matches = search_file(
939939
abs_path, pattern, fuzzy, regex, case_sensitive, invert
940940
)
941-
except Exception:
941+
except (OSError, UnicodeDecodeError):
942942
continue # skip unreadable / binary files silently
943943

944944
files_searched += 1

tests/unit/test_tools_search_directory.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55
from unittest.mock import patch
66

7+
from src.exceptions import SearchError
78
from src.tools import search_directory
89

910
# ---------------------------------------------------------------------------
@@ -322,7 +323,7 @@ def test_fuzzy_mode_finds_approximate_match(self, tmp_path: Path) -> None:
322323

323324
class TestSearchDirectoryErrorPaths:
324325
def test_unreadable_file_is_skipped_silently(self, tmp_path: Path) -> None:
325-
"""search_file raising any exception causes the file to be silently skipped."""
326+
"""OSError from search_file (unreadable / binary) causes the file to be skipped."""
326327
(tmp_path / "good.txt").write_text("needle\n")
327328
(tmp_path / "bad.txt").write_text("needle\n")
328329

@@ -373,3 +374,23 @@ def test_read_file_lines_failure_yields_empty_context(self, tmp_path: Path) -> N
373374
m = result["results"][0]["matches"][0]
374375
assert m["context_before"] == []
375376
assert m["context_after"] == []
377+
378+
def test_search_error_propagates_as_error_dict(self, tmp_path: Path) -> None:
379+
"""SearchError (invalid regex, regex+fuzzy) is NOT swallowed — propagates
380+
through @handle_tool_errors and surfaces as an error dict."""
381+
(tmp_path / "f.txt").write_text("some content\n")
382+
383+
with patch(
384+
"src.tools.search_file",
385+
side_effect=SearchError("regex+fuzzy not allowed"),
386+
):
387+
result = search_directory(str(tmp_path), ".*", regex=True)
388+
389+
assert "error" in result
390+
assert "regex+fuzzy not allowed" in result["error"]
391+
392+
def test_invalid_regex_returns_error_dict(self, tmp_path: Path) -> None:
393+
"""An invalid regex pattern surfaces as an error dict, not empty results."""
394+
(tmp_path / "f.txt").write_text("hello\n")
395+
result = search_directory(str(tmp_path), "[unclosed", regex=True)
396+
assert "error" in result

0 commit comments

Comments
 (0)