Skip to content

Commit fe883cc

Browse files
Wrap unexpected file path check runtime errors
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 57ed2a3 commit fe883cc

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

hyperbrowser/client/file_utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,21 @@ def ensure_existing_file_path(
3838
raise HyperbrowserError("file_path must not contain control characters")
3939
try:
4040
path_exists = os.path.exists(normalized_path)
41-
except (OSError, ValueError) as exc:
41+
except HyperbrowserError:
42+
raise
43+
except (OSError, ValueError, TypeError) as exc:
44+
raise HyperbrowserError("file_path is invalid", original_error=exc) from exc
45+
except Exception as exc:
4246
raise HyperbrowserError("file_path is invalid", original_error=exc) from exc
4347
if not path_exists:
4448
raise HyperbrowserError(missing_file_message)
4549
try:
4650
is_file = os.path.isfile(normalized_path)
47-
except (OSError, ValueError) as exc:
51+
except HyperbrowserError:
52+
raise
53+
except (OSError, ValueError, TypeError) as exc:
54+
raise HyperbrowserError("file_path is invalid", original_error=exc) from exc
55+
except Exception as exc:
4856
raise HyperbrowserError("file_path is invalid", original_error=exc) from exc
4957
if not is_file:
5058
raise HyperbrowserError(not_file_message)

tests/test_file_utils.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,45 @@ def raising_exists(path: str) -> bool:
136136
)
137137

138138

139+
def test_ensure_existing_file_path_wraps_unexpected_exists_errors(monkeypatch):
140+
def raising_exists(path: str) -> bool:
141+
_ = path
142+
raise RuntimeError("unexpected exists failure")
143+
144+
monkeypatch.setattr(file_utils.os.path, "exists", raising_exists)
145+
146+
with pytest.raises(HyperbrowserError, match="file_path is invalid") as exc_info:
147+
ensure_existing_file_path(
148+
"/tmp/maybe-invalid",
149+
missing_file_message="missing",
150+
not_file_message="not-file",
151+
)
152+
153+
assert exc_info.value.original_error is not None
154+
155+
156+
def test_ensure_existing_file_path_wraps_unexpected_isfile_errors(
157+
monkeypatch, tmp_path: Path
158+
):
159+
file_path = tmp_path / "target.txt"
160+
file_path.write_text("content")
161+
162+
def raising_isfile(path: str) -> bool:
163+
_ = path
164+
raise RuntimeError("unexpected isfile failure")
165+
166+
monkeypatch.setattr(file_utils.os.path, "isfile", raising_isfile)
167+
168+
with pytest.raises(HyperbrowserError, match="file_path is invalid") as exc_info:
169+
ensure_existing_file_path(
170+
str(file_path),
171+
missing_file_message="missing",
172+
not_file_message="not-file",
173+
)
174+
175+
assert exc_info.value.original_error is not None
176+
177+
139178
def test_ensure_existing_file_path_wraps_fspath_runtime_errors():
140179
class _BrokenPathLike:
141180
def __fspath__(self) -> str:

0 commit comments

Comments
 (0)