Skip to content

Commit bf510d5

Browse files
Wrap unexpected base URL parsing failures
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 985697b commit bf510d5

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

hyperbrowser/config.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ def normalize_base_url(base_url: str) -> str:
6767
):
6868
raise HyperbrowserError("base_url must not contain control characters")
6969

70-
parsed_base_url = urlparse(normalized_base_url)
70+
try:
71+
parsed_base_url = urlparse(normalized_base_url)
72+
except HyperbrowserError:
73+
raise
74+
except Exception as exc:
75+
raise HyperbrowserError(
76+
"Failed to parse base_url",
77+
original_error=exc,
78+
) from exc
7179
if (
7280
parsed_base_url.scheme not in {"https", "http"}
7381
or not parsed_base_url.netloc

tests/test_config.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,34 @@ def test_client_config_normalize_base_url_validates_and_normalizes():
431431
ClientConfig.normalize_base_url("https://user:pass@example.local")
432432

433433

434+
def test_client_config_normalize_base_url_wraps_unexpected_urlparse_errors(
435+
monkeypatch: pytest.MonkeyPatch,
436+
):
437+
def _raise_runtime_error(_value: str):
438+
raise RuntimeError("url parser exploded")
439+
440+
monkeypatch.setattr(config_module, "urlparse", _raise_runtime_error)
441+
442+
with pytest.raises(HyperbrowserError, match="Failed to parse base_url") as exc_info:
443+
ClientConfig.normalize_base_url("https://example.local")
444+
445+
assert exc_info.value.original_error is not None
446+
447+
448+
def test_client_config_normalize_base_url_preserves_hyperbrowser_urlparse_errors(
449+
monkeypatch: pytest.MonkeyPatch,
450+
):
451+
def _raise_hyperbrowser_error(_value: str):
452+
raise HyperbrowserError("custom urlparse failure")
453+
454+
monkeypatch.setattr(config_module, "urlparse", _raise_hyperbrowser_error)
455+
456+
with pytest.raises(HyperbrowserError, match="custom urlparse failure") as exc_info:
457+
ClientConfig.normalize_base_url("https://example.local")
458+
459+
assert exc_info.value.original_error is None
460+
461+
434462
def test_client_config_normalize_base_url_preserves_invalid_port_original_error():
435463
with pytest.raises(
436464
HyperbrowserError, match="base_url must contain a valid port number"

0 commit comments

Comments
 (0)