Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional context in InvalidURL exceptions. #2675

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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