Skip to content

Commit 34a498a

Browse files
Wrap response operation-name empty-check length failures
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent ba0324c commit 34a498a

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

hyperbrowser/client/managers/response_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ def parse_response_model(
6464
normalized_operation_name_input = operation_name.strip()
6565
if not isinstance(normalized_operation_name_input, str):
6666
raise TypeError("normalized operation_name must be a string")
67+
is_empty_operation_name = len(normalized_operation_name_input) == 0
6768
except HyperbrowserError:
6869
raise
6970
except Exception as exc:
7071
raise HyperbrowserError(
7172
"Failed to normalize operation_name",
7273
original_error=exc,
7374
) from exc
74-
if not normalized_operation_name_input:
75+
if is_empty_operation_name:
7576
raise HyperbrowserError("operation_name must be a non-empty string")
7677
normalized_operation_name = _normalize_operation_name_for_error(operation_name)
7778
if not isinstance(response_data, Mapping):

tests/test_response_utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,48 @@ def strip(self, chars=None): # type: ignore[override]
256256
assert isinstance(exc_info.value.original_error, TypeError)
257257

258258

259+
def test_parse_response_model_wraps_operation_name_empty_check_length_failures():
260+
class _BrokenOperationName(str):
261+
class _NormalizedName(str):
262+
def __len__(self):
263+
raise RuntimeError("operation name length exploded")
264+
265+
def strip(self, chars=None): # type: ignore[override]
266+
_ = chars
267+
return self._NormalizedName("basic operation")
268+
269+
with pytest.raises(HyperbrowserError, match="Failed to normalize operation_name") as exc_info:
270+
parse_response_model(
271+
{"success": True},
272+
model=BasicResponse,
273+
operation_name=_BrokenOperationName("basic operation"),
274+
)
275+
276+
assert isinstance(exc_info.value.original_error, RuntimeError)
277+
278+
279+
def test_parse_response_model_preserves_hyperbrowser_operation_name_empty_check_length_failures():
280+
class _BrokenOperationName(str):
281+
class _NormalizedName(str):
282+
def __len__(self):
283+
raise HyperbrowserError("custom operation name length failure")
284+
285+
def strip(self, chars=None): # type: ignore[override]
286+
_ = chars
287+
return self._NormalizedName("basic operation")
288+
289+
with pytest.raises(
290+
HyperbrowserError, match="custom operation name length failure"
291+
) as exc_info:
292+
parse_response_model(
293+
{"success": True},
294+
model=BasicResponse,
295+
operation_name=_BrokenOperationName("basic operation"),
296+
)
297+
298+
assert exc_info.value.original_error is None
299+
300+
259301
def test_parse_response_model_truncates_operation_name_in_errors():
260302
long_operation_name = "basic operation " + ("x" * 200)
261303

0 commit comments

Comments
 (0)