Additional context in InvalidURL exceptions (#2675)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tom Christie · committed by GitHub · parent cca6206 · Browse files · 2 files·+8 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+8 added−8 removed
- tests:+4 added−4 removed
- other:+4 added−4 removed
Provenance
- Commit
26dc392- Tree
d069e4c- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
cca6206
2 files changed+8 added−8 removed
modified:httpx/_urlparse.py+4 added-4 removed
285 unmodified lines286 # IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet287 try:288 ipaddress.IPv4Address(host)289 except ipaddress.AddressValueError:290- raise InvalidURL("Invalid IPv4 address")290+ raise InvalidURL(f"Invalid IPv4 address: {host!r}")291 return host292 293 elif IPv6_STYLE_HOSTNAME.match(host):294 # Validate IPv6 hostnames like [...]6 unmodified lines301 # square bracket characters are allowed in the URI syntax."302 try:303 ipaddress.IPv6Address(host[1:-1])304 except ipaddress.AddressValueError:305- raise InvalidURL("Invalid IPv6 address")305+ raise InvalidURL(f"Invalid IPv6 address: {host!r}")306 return host[1:-1]307 308 elif host.isascii():309 # Regular ASCII hostnames6 unmodified lines316 # IDNA hostnames317 try:318 return idna.encode(host.lower()).decode("ascii")319 except idna.IDNAError:320- raise InvalidURL("Invalid IDNA hostname")320+ raise InvalidURL(f"Invalid IDNA hostname: {host!r}")321 322 323 def normalize_port(324 port: typing.Optional[typing.Union[str, int]], scheme: str12 unmodified lines337 338 try:339 port_as_int = int(port)340 except ValueError:341- raise InvalidURL("Invalid port")341+ raise InvalidURL(f"Invalid port: {port!r}")342 343 # See https://url.spec.whatwg.org/#url-miscellaneous344 default_port = {"ftp": 21, "http": 80, "https": 443, "ws": 80, "wss": 443}.get(345 scheme110 unmodified linesmodified:tests/test_urlparse.py+4 added-4 removed
51 unmodified lines52 53 def test_urlparse_invalid_ipv4():54 with pytest.raises(httpx.InvalidURL) as exc:55 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'"57 58 59 def test_urlparse_valid_ipv6():60 url = httpx.URL("https://[2001:db8::ff00:42:8329]/")2 unmodified lines63 64 def test_urlparse_invalid_ipv6():65 with pytest.raises(httpx.InvalidURL) as exc:66 httpx.URL("https://[2001]/")67- assert str(exc.value) == "Invalid IPv6 address"67+ assert str(exc.value) == "Invalid IPv6 address: '[2001]'"68 69 70 def test_urlparse_unescaped_idna_host():71 url = httpx.URL("https://中国.icom.museum/")7 unmodified lines79 80 def test_urlparse_invalid_idna_host():81 with pytest.raises(httpx.InvalidURL) as exc:82 httpx.URL("https://☃.com/")83- assert str(exc.value) == "Invalid IDNA hostname"83+ assert str(exc.value) == "Invalid IDNA hostname: '☃.com'"84 85 86 # Tests for different port types87 11 unmodified lines99 100 def test_urlparse_invalid_port():101 with pytest.raises(httpx.InvalidURL) as exc:102 httpx.URL("https://example.com:abc/")103- assert str(exc.value) == "Invalid port"103+ assert str(exc.value) == "Invalid port: 'abc'"104 105 106 # Tests for path handling107 142 unmodified lines