Skip to content

Commit 41b3ac1

Browse files
Wrap computer action param serialization failures
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 080cb42 commit 41b3ac1

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

hyperbrowser/client/managers/async_manager/computer_action.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,15 @@ async def _execute_request(
9191
)
9292

9393
if isinstance(params, BaseModel):
94-
payload = params.model_dump(by_alias=True, exclude_none=True)
94+
try:
95+
payload = params.model_dump(by_alias=True, exclude_none=True)
96+
except HyperbrowserError:
97+
raise
98+
except Exception as exc:
99+
raise HyperbrowserError(
100+
"Failed to serialize computer action params",
101+
original_error=exc,
102+
) from exc
95103
else:
96104
payload = params
97105

hyperbrowser/client/managers/sync_manager/computer_action.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,15 @@ def _execute_request(
9191
)
9292

9393
if isinstance(params, BaseModel):
94-
payload = params.model_dump(by_alias=True, exclude_none=True)
94+
try:
95+
payload = params.model_dump(by_alias=True, exclude_none=True)
96+
except HyperbrowserError:
97+
raise
98+
except Exception as exc:
99+
raise HyperbrowserError(
100+
"Failed to serialize computer action params",
101+
original_error=exc,
102+
) from exc
95103
else:
96104
payload = params
97105

tests/test_computer_action_manager.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from types import SimpleNamespace
33

44
import pytest
5+
from pydantic import BaseModel
56

67
from hyperbrowser.client.managers.async_manager.computer_action import (
78
ComputerActionManager as AsyncComputerActionManager,
@@ -160,3 +161,79 @@ async def run() -> None:
160161
await manager.screenshot(_SessionId("sess_123"))
161162

162163
asyncio.run(run())
164+
165+
166+
def test_sync_computer_action_manager_wraps_param_serialization_errors():
167+
class _BrokenParams(BaseModel):
168+
def model_dump(self, *args, **kwargs): # type: ignore[override]
169+
_ = args
170+
_ = kwargs
171+
raise RuntimeError("broken model_dump")
172+
173+
manager = SyncComputerActionManager(_DummyClient())
174+
session = SimpleNamespace(computer_action_endpoint="https://example.com/cua")
175+
176+
with pytest.raises(
177+
HyperbrowserError, match="Failed to serialize computer action params"
178+
) as exc_info:
179+
manager._execute_request(session, _BrokenParams()) # type: ignore[arg-type]
180+
181+
assert isinstance(exc_info.value.original_error, RuntimeError)
182+
183+
184+
def test_sync_computer_action_manager_preserves_hyperbrowser_param_serialization_errors():
185+
class _BrokenParams(BaseModel):
186+
def model_dump(self, *args, **kwargs): # type: ignore[override]
187+
_ = args
188+
_ = kwargs
189+
raise HyperbrowserError("custom model_dump failure")
190+
191+
manager = SyncComputerActionManager(_DummyClient())
192+
session = SimpleNamespace(computer_action_endpoint="https://example.com/cua")
193+
194+
with pytest.raises(
195+
HyperbrowserError, match="custom model_dump failure"
196+
) as exc_info:
197+
manager._execute_request(session, _BrokenParams()) # type: ignore[arg-type]
198+
199+
assert exc_info.value.original_error is None
200+
201+
202+
def test_async_computer_action_manager_wraps_param_serialization_errors():
203+
class _BrokenParams(BaseModel):
204+
def model_dump(self, *args, **kwargs): # type: ignore[override]
205+
_ = args
206+
_ = kwargs
207+
raise RuntimeError("broken model_dump")
208+
209+
manager = AsyncComputerActionManager(_DummyClient())
210+
session = SimpleNamespace(computer_action_endpoint="https://example.com/cua")
211+
212+
async def run() -> None:
213+
with pytest.raises(
214+
HyperbrowserError, match="Failed to serialize computer action params"
215+
) as exc_info:
216+
await manager._execute_request(session, _BrokenParams()) # type: ignore[arg-type]
217+
assert isinstance(exc_info.value.original_error, RuntimeError)
218+
219+
asyncio.run(run())
220+
221+
222+
def test_async_computer_action_manager_preserves_hyperbrowser_param_serialization_errors():
223+
class _BrokenParams(BaseModel):
224+
def model_dump(self, *args, **kwargs): # type: ignore[override]
225+
_ = args
226+
_ = kwargs
227+
raise HyperbrowserError("custom model_dump failure")
228+
229+
manager = AsyncComputerActionManager(_DummyClient())
230+
session = SimpleNamespace(computer_action_endpoint="https://example.com/cua")
231+
232+
async def run() -> None:
233+
with pytest.raises(
234+
HyperbrowserError, match="custom model_dump failure"
235+
) as exc_info:
236+
await manager._execute_request(session, _BrokenParams()) # type: ignore[arg-type]
237+
assert exc_info.value.original_error is None
238+
239+
asyncio.run(run())

0 commit comments

Comments
 (0)