Skip to content

Commit 3511424

Browse files
Wrap invalid PathLike state in session upload managers
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent d69e90f commit 3511424

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

hyperbrowser/client/managers/async_manager/session.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,15 @@ async def upload_file(
151151
self, id: str, file_input: Union[str, PathLike[str], IO]
152152
) -> UploadFileResponse:
153153
if isinstance(file_input, (str, PathLike)):
154-
raw_file_path = os.fspath(file_input)
154+
try:
155+
raw_file_path = os.fspath(file_input)
156+
except HyperbrowserError:
157+
raise
158+
except Exception as exc:
159+
raise HyperbrowserError(
160+
"file_input path is invalid",
161+
original_error=exc,
162+
) from exc
155163
file_path = ensure_existing_file_path(
156164
raw_file_path,
157165
missing_file_message=f"Upload file not found at path: {raw_file_path}",

hyperbrowser/client/managers/sync_manager/session.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,15 @@ def upload_file(
143143
self, id: str, file_input: Union[str, PathLike[str], IO]
144144
) -> UploadFileResponse:
145145
if isinstance(file_input, (str, PathLike)):
146-
raw_file_path = os.fspath(file_input)
146+
try:
147+
raw_file_path = os.fspath(file_input)
148+
except HyperbrowserError:
149+
raise
150+
except Exception as exc:
151+
raise HyperbrowserError(
152+
"file_input path is invalid",
153+
original_error=exc,
154+
) from exc
147155
file_path = ensure_existing_file_path(
148156
raw_file_path,
149157
missing_file_message=f"Upload file not found at path: {raw_file_path}",

tests/test_session_upload_file.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import io
3+
from os import PathLike
34
from pathlib import Path
45
import pytest
56

@@ -140,6 +141,38 @@ async def run():
140141
asyncio.run(run())
141142

142143

144+
def test_sync_session_upload_file_wraps_invalid_pathlike_state_errors():
145+
manager = SyncSessionManager(_FakeClient(_SyncTransport()))
146+
147+
class _BrokenPathLike(PathLike[str]):
148+
def __fspath__(self) -> str:
149+
raise RuntimeError("broken fspath")
150+
151+
with pytest.raises(
152+
HyperbrowserError, match="file_input path is invalid"
153+
) as exc_info:
154+
manager.upload_file("session_123", _BrokenPathLike())
155+
156+
assert isinstance(exc_info.value.original_error, RuntimeError)
157+
158+
159+
def test_async_session_upload_file_wraps_invalid_pathlike_state_errors():
160+
manager = AsyncSessionManager(_FakeClient(_AsyncTransport()))
161+
162+
class _BrokenPathLike(PathLike[str]):
163+
def __fspath__(self) -> str:
164+
raise RuntimeError("broken fspath")
165+
166+
async def run():
167+
with pytest.raises(
168+
HyperbrowserError, match="file_input path is invalid"
169+
) as exc_info:
170+
await manager.upload_file("session_123", _BrokenPathLike())
171+
assert isinstance(exc_info.value.original_error, RuntimeError)
172+
173+
asyncio.run(run())
174+
175+
143176
def test_sync_session_upload_file_rejects_non_callable_read_attribute():
144177
manager = SyncSessionManager(_FakeClient(_SyncTransport()))
145178
fake_file = type("FakeFile", (), {"read": "not-callable"})()

0 commit comments

Comments
 (0)