Skip to content

Commit

Permalink
Additional context in InvalidURL exceptions (#2675)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie committed Apr 20, 2023
1 parent cca6206 commit 26dc392
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions httpx/_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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():
Expand All @@ -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(
Expand All @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions tests/test_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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():
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 26dc392

Please sign in to comment.