Skip to content

Commit 57ed2a3

Browse files
Wrap PathLike fspath runtime failures
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent a463d53 commit 57ed2a3

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

hyperbrowser/client/file_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ def ensure_existing_file_path(
1313
) -> str:
1414
try:
1515
normalized_path = os.fspath(file_path)
16+
except HyperbrowserError:
17+
raise
1618
except TypeError as exc:
1719
raise HyperbrowserError(
1820
"file_path must be a string or os.PathLike object",
1921
original_error=exc,
2022
) from exc
23+
except Exception as exc:
24+
raise HyperbrowserError("file_path is invalid", original_error=exc) from exc
2125
if not isinstance(normalized_path, str):
2226
raise HyperbrowserError("file_path must resolve to a string path")
2327
if not normalized_path.strip():

tests/test_file_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,33 @@ def raising_exists(path: str) -> bool:
134134
missing_file_message="missing",
135135
not_file_message="not-file",
136136
)
137+
138+
139+
def test_ensure_existing_file_path_wraps_fspath_runtime_errors():
140+
class _BrokenPathLike:
141+
def __fspath__(self) -> str:
142+
raise RuntimeError("bad fspath")
143+
144+
with pytest.raises(HyperbrowserError, match="file_path is invalid") as exc_info:
145+
ensure_existing_file_path(
146+
_BrokenPathLike(), # type: ignore[arg-type]
147+
missing_file_message="missing",
148+
not_file_message="not-file",
149+
)
150+
151+
assert exc_info.value.original_error is not None
152+
153+
154+
def test_ensure_existing_file_path_preserves_hyperbrowser_fspath_errors():
155+
class _BrokenPathLike:
156+
def __fspath__(self) -> str:
157+
raise HyperbrowserError("custom fspath failure")
158+
159+
with pytest.raises(HyperbrowserError, match="custom fspath failure") as exc_info:
160+
ensure_existing_file_path(
161+
_BrokenPathLike(), # type: ignore[arg-type]
162+
missing_file_message="missing",
163+
not_file_message="not-file",
164+
)
165+
166+
assert exc_info.value.original_error is None

0 commit comments

Comments
 (0)