Skip to content

Commit 080cb42

Browse files
Wrap update_profile_params serialization failures
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 0990760 commit 080cb42

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

hyperbrowser/client/managers/async_manager/session.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,23 @@ async def update_profile_params(
276276
"update_profile_params() requires either UpdateSessionProfileParams or a boolean persist_changes."
277277
)
278278

279+
try:
280+
serialized_params = params_obj.model_dump(exclude_none=True, by_alias=True)
281+
except HyperbrowserError:
282+
raise
283+
except Exception as exc:
284+
raise HyperbrowserError(
285+
"Failed to serialize update_profile_params payload",
286+
original_error=exc,
287+
) from exc
288+
if type(serialized_params) is not dict:
289+
raise HyperbrowserError("Failed to serialize update_profile_params payload")
290+
279291
response = await self._client.transport.put(
280292
self._client._build_url(f"/session/{id}/update"),
281293
data={
282294
"type": "profile",
283-
"params": params_obj.model_dump(exclude_none=True, by_alias=True),
295+
"params": serialized_params,
284296
},
285297
)
286298
return parse_session_response_model(

hyperbrowser/client/managers/sync_manager/session.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,23 @@ def update_profile_params(
268268
"update_profile_params() requires either UpdateSessionProfileParams or a boolean persist_changes."
269269
)
270270

271+
try:
272+
serialized_params = params_obj.model_dump(exclude_none=True, by_alias=True)
273+
except HyperbrowserError:
274+
raise
275+
except Exception as exc:
276+
raise HyperbrowserError(
277+
"Failed to serialize update_profile_params payload",
278+
original_error=exc,
279+
) from exc
280+
if type(serialized_params) is not dict:
281+
raise HyperbrowserError("Failed to serialize update_profile_params payload")
282+
271283
response = self._client.transport.put(
272284
self._client._build_url(f"/session/{id}/update"),
273285
data={
274286
"type": "profile",
275-
"params": params_obj.model_dump(exclude_none=True, by_alias=True),
287+
"params": serialized_params,
276288
},
277289
)
278290
return parse_session_response_model(

tests/test_session_update_profile_params.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,52 @@ class _Params(UpdateSessionProfileParams):
105105
manager.update_profile_params("session-1", _Params(persist_changes=True))
106106

107107

108+
def test_sync_update_profile_params_wraps_serialization_errors(
109+
monkeypatch: pytest.MonkeyPatch,
110+
):
111+
manager = SyncSessionManager(_SyncClient())
112+
params = UpdateSessionProfileParams(persist_changes=True)
113+
114+
def _raise_model_dump_error(*args, **kwargs):
115+
_ = args
116+
_ = kwargs
117+
raise RuntimeError("broken model_dump")
118+
119+
monkeypatch.setattr(
120+
UpdateSessionProfileParams, "model_dump", _raise_model_dump_error
121+
)
122+
123+
with pytest.raises(
124+
HyperbrowserError, match="Failed to serialize update_profile_params payload"
125+
) as exc_info:
126+
manager.update_profile_params("session-1", params)
127+
128+
assert isinstance(exc_info.value.original_error, RuntimeError)
129+
130+
131+
def test_sync_update_profile_params_preserves_hyperbrowser_serialization_errors(
132+
monkeypatch: pytest.MonkeyPatch,
133+
):
134+
manager = SyncSessionManager(_SyncClient())
135+
params = UpdateSessionProfileParams(persist_changes=True)
136+
137+
def _raise_model_dump_error(*args, **kwargs):
138+
_ = args
139+
_ = kwargs
140+
raise HyperbrowserError("custom model_dump failure")
141+
142+
monkeypatch.setattr(
143+
UpdateSessionProfileParams, "model_dump", _raise_model_dump_error
144+
)
145+
146+
with pytest.raises(
147+
HyperbrowserError, match="custom model_dump failure"
148+
) as exc_info:
149+
manager.update_profile_params("session-1", params)
150+
151+
assert exc_info.value.original_error is None
152+
153+
108154
def test_async_update_profile_params_bool_warns_and_serializes():
109155
AsyncSessionManager._has_warned_update_profile_params_boolean_deprecated = False
110156
client = _AsyncClient()
@@ -164,6 +210,56 @@ async def run() -> None:
164210
asyncio.run(run())
165211

166212

213+
def test_async_update_profile_params_wraps_serialization_errors(
214+
monkeypatch: pytest.MonkeyPatch,
215+
):
216+
manager = AsyncSessionManager(_AsyncClient())
217+
params = UpdateSessionProfileParams(persist_changes=True)
218+
219+
def _raise_model_dump_error(*args, **kwargs):
220+
_ = args
221+
_ = kwargs
222+
raise RuntimeError("broken model_dump")
223+
224+
monkeypatch.setattr(
225+
UpdateSessionProfileParams, "model_dump", _raise_model_dump_error
226+
)
227+
228+
async def run() -> None:
229+
with pytest.raises(
230+
HyperbrowserError, match="Failed to serialize update_profile_params payload"
231+
) as exc_info:
232+
await manager.update_profile_params("session-1", params)
233+
assert isinstance(exc_info.value.original_error, RuntimeError)
234+
235+
asyncio.run(run())
236+
237+
238+
def test_async_update_profile_params_preserves_hyperbrowser_serialization_errors(
239+
monkeypatch: pytest.MonkeyPatch,
240+
):
241+
manager = AsyncSessionManager(_AsyncClient())
242+
params = UpdateSessionProfileParams(persist_changes=True)
243+
244+
def _raise_model_dump_error(*args, **kwargs):
245+
_ = args
246+
_ = kwargs
247+
raise HyperbrowserError("custom model_dump failure")
248+
249+
monkeypatch.setattr(
250+
UpdateSessionProfileParams, "model_dump", _raise_model_dump_error
251+
)
252+
253+
async def run() -> None:
254+
with pytest.raises(
255+
HyperbrowserError, match="custom model_dump failure"
256+
) as exc_info:
257+
await manager.update_profile_params("session-1", params)
258+
assert exc_info.value.original_error is None
259+
260+
asyncio.run(run())
261+
262+
167263
def test_sync_update_profile_params_requires_argument_or_keyword():
168264
manager = SyncSessionManager(_SyncClient())
169265

0 commit comments

Comments
 (0)