Skip to content

Commit 26dc392

Browse files
authored
Additional context in InvalidURL exceptions (#2675)
1 parent cca6206 commit 26dc392

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

httpx/_urlparse.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def encode_host(host: str) -> str:
287287
try:
288288
ipaddress.IPv4Address(host)
289289
except ipaddress.AddressValueError:
290-
raise InvalidURL("Invalid IPv4 address")
290+
raise InvalidURL(f"Invalid IPv4 address: {host!r}")
291291
return host
292292

293293
elif IPv6_STYLE_HOSTNAME.match(host):
@@ -302,7 +302,7 @@ def encode_host(host: str) -> str:
302302
try:
303303
ipaddress.IPv6Address(host[1:-1])
304304
except ipaddress.AddressValueError:
305-
raise InvalidURL("Invalid IPv6 address")
305+
raise InvalidURL(f"Invalid IPv6 address: {host!r}")
306306
return host[1:-1]
307307

308308
elif host.isascii():
@@ -317,7 +317,7 @@ def encode_host(host: str) -> str:
317317
try:
318318
return idna.encode(host.lower()).decode("ascii")
319319
except idna.IDNAError:
320-
raise InvalidURL("Invalid IDNA hostname")
320+
raise InvalidURL(f"Invalid IDNA hostname: {host!r}")
321321

322322

323323
def normalize_port(
@@ -338,7 +338,7 @@ def normalize_port(
338338
try:
339339
port_as_int = int(port)
340340
except ValueError:
341-
raise InvalidURL("Invalid port")
341+
raise InvalidURL(f"Invalid port: {port!r}")
342342

343343
# See https://url.spec.whatwg.org/#url-miscellaneous
344344
default_port = {"ftp": 21, "http": 80, "https": 443, "ws": 80, "wss": 443}.get(

tests/test_urlparse.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_urlparse_valid_ipv4():
5353
def test_urlparse_invalid_ipv4():
5454
with pytest.raises(httpx.InvalidURL) as exc:
5555
httpx.URL("https://999.999.999.999/")
56-
assert str(exc.value) == "Invalid IPv4 address"
56+
assert str(exc.value) == "Invalid IPv4 address: '999.999.999.999'"
5757

5858

5959
def test_urlparse_valid_ipv6():
@@ -64,7 +64,7 @@ def test_urlparse_valid_ipv6():
6464
def test_urlparse_invalid_ipv6():
6565
with pytest.raises(httpx.InvalidURL) as exc:
6666
httpx.URL("https://[2001]/")
67-
assert str(exc.value) == "Invalid IPv6 address"
67+
assert str(exc.value) == "Invalid IPv6 address: '[2001]'"
6868

6969

7070
def test_urlparse_unescaped_idna_host():
@@ -80,7 +80,7 @@ def test_urlparse_escaped_idna_host():
8080
def test_urlparse_invalid_idna_host():
8181
with pytest.raises(httpx.InvalidURL) as exc:
8282
httpx.URL("https://☃.com/")
83-
assert str(exc.value) == "Invalid IDNA hostname"
83+
assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"
8484

8585

8686
# Tests for different port types
@@ -100,7 +100,7 @@ def test_urlparse_normalized_port():
100100
def test_urlparse_invalid_port():
101101
with pytest.raises(httpx.InvalidURL) as exc:
102102
httpx.URL("https://example.com:abc/")
103-
assert str(exc.value) == "Invalid port"
103+
assert str(exc.value) == "Invalid port: 'abc'"
104104

105105

106106
# Tests for path handling

0 commit comments

Comments
 (0)