]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Additional context in InvalidURL exceptions (#2675)
authorTom Christie <tom@tomchristie.com>
Thu, 20 Apr 2023 11:17:44 +0000 (12:17 +0100)
committerGitHub <noreply@github.com>
Thu, 20 Apr 2023 11:17:44 +0000 (12:17 +0100)
httpx/_urlparse.py
tests/test_urlparse.py

index 6522d917bc90a8c091d42570d581169da4c5e4ad..5ee6e58201dfa2a19f0d7c5cf14609a4e03c1a90 100644 (file)
@@ -287,7 +287,7 @@ def encode_host(host: str) -> str:
         try:
             ipaddress.IPv4Address(host)
         except ipaddress.AddressValueError:
-            raise InvalidURL("Invalid IPv4 address")
+            raise InvalidURL(f"Invalid IPv4 address: {host!r}")
         return host
 
     elif IPv6_STYLE_HOSTNAME.match(host):
@@ -302,7 +302,7 @@ def encode_host(host: str) -> str:
         try:
             ipaddress.IPv6Address(host[1:-1])
         except ipaddress.AddressValueError:
-            raise InvalidURL("Invalid IPv6 address")
+            raise InvalidURL(f"Invalid IPv6 address: {host!r}")
         return host[1:-1]
 
     elif host.isascii():
@@ -317,7 +317,7 @@ def encode_host(host: str) -> str:
     try:
         return idna.encode(host.lower()).decode("ascii")
     except idna.IDNAError:
-        raise InvalidURL("Invalid IDNA hostname")
+        raise InvalidURL(f"Invalid IDNA hostname: {host!r}")
 
 
 def normalize_port(
@@ -338,7 +338,7 @@ def normalize_port(
     try:
         port_as_int = int(port)
     except ValueError:
-        raise InvalidURL("Invalid port")
+        raise InvalidURL(f"Invalid port: {port!r}")
 
     # See https://url.spec.whatwg.org/#url-miscellaneous
     default_port = {"ftp": 21, "http": 80, "https": 443, "ws": 80, "wss": 443}.get(
index 6b5c83442790ef2ae9b30691af7118f6f42f11dc..575ec84a8bee38d9ffcee8c461c7d60d9254daf6 100644 (file)
@@ -53,7 +53,7 @@ def test_urlparse_valid_ipv4():
 def test_urlparse_invalid_ipv4():
     with pytest.raises(httpx.InvalidURL) as exc:
         httpx.URL("https://999.999.999.999/")
-    assert str(exc.value) == "Invalid IPv4 address"
+    assert str(exc.value) == "Invalid IPv4 address: '999.999.999.999'"
 
 
 def test_urlparse_valid_ipv6():
@@ -64,7 +64,7 @@ def test_urlparse_valid_ipv6():
 def test_urlparse_invalid_ipv6():
     with pytest.raises(httpx.InvalidURL) as exc:
         httpx.URL("https://[2001]/")
-    assert str(exc.value) == "Invalid IPv6 address"
+    assert str(exc.value) == "Invalid IPv6 address: '[2001]'"
 
 
 def test_urlparse_unescaped_idna_host():
@@ -80,7 +80,7 @@ def test_urlparse_escaped_idna_host():
 def test_urlparse_invalid_idna_host():
     with pytest.raises(httpx.InvalidURL) as exc:
         httpx.URL("https://☃.com/")
-    assert str(exc.value) == "Invalid IDNA hostname"
+    assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"
 
 
 # Tests for different port types
@@ -100,7 +100,7 @@ def test_urlparse_normalized_port():
 def test_urlparse_invalid_port():
     with pytest.raises(httpx.InvalidURL) as exc:
         httpx.URL("https://example.com:abc/")
-    assert str(exc.value) == "Invalid port"
+    assert str(exc.value) == "Invalid port: 'abc'"
 
 
 # Tests for path handling