Skip to content

Commit 78150be

Browse files
Require concrete exception text render outputs
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent b719fa8 commit 78150be

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

hyperbrowser/exceptions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ def _safe_exception_text(value: Any, *, fallback: str) -> str:
2121
text_value = str(value)
2222
except Exception:
2323
return fallback
24-
if not isinstance(text_value, str):
24+
if type(text_value) is not str:
2525
return fallback
2626
try:
2727
sanitized_value = "".join(
2828
"?" if ord(character) < 32 or ord(character) == 127 else character
2929
for character in text_value
3030
)
31+
if type(sanitized_value) is not str:
32+
return fallback
3133
if sanitized_value.strip():
3234
return _truncate_exception_text(sanitized_value)
3335
except Exception:

tests/test_exceptions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,16 @@ def __str__(self) -> str:
9898
str(error)
9999
== "request failed - Caused by _BrokenOriginalError: <unstringifiable _BrokenOriginalError>"
100100
)
101+
102+
103+
def test_hyperbrowser_error_str_falls_back_for_string_subclass_str_results():
104+
class _BrokenMessage:
105+
class _SubclassString(str):
106+
pass
107+
108+
def __str__(self) -> str:
109+
return self._SubclassString("request failed")
110+
111+
error = HyperbrowserError(_BrokenMessage()) # type: ignore[arg-type]
112+
113+
assert str(error) == "Hyperbrowser error"

0 commit comments

Comments
 (0)