encode/httpx

Introduce `httpx.SSLError` exception

pquentin opened this issue · 2 comments

httpcore 1.0.6 was released with a change that now maps SSLError to httpcore.ConnectError. But I'm realizing now that this change only affects asyncio (with and without anyio). All other backends (sync, trio with and without anyio) already raised httpx.ConnectError. The current behavior is consistent across backends, as is shown by the following tests:

import asyncio
import contextlib
import traceback

import anyio
import httpx
import pytest
import trio


def sync_bad_ssl():
    client = httpx.Client()
    client.get("https://tls-v1-0.badssl.com:1010")


async def bad_ssl():
    client = httpx.AsyncClient()
    await client.get("https://tls-v1-0.badssl.com:1010")


def test_sync_bad_ssl():
    with pytest.raises(httpx.ConnectError):
        sync_bad_ssl()

# Fails with httpcore<1.0.6
@pytest.mark.asyncio
async def test_bad_ssl_asyncio():
    with pytest.raises(httpx.ConnectError):
        await bad_ssl()


@pytest.mark.trio
async def test_bad_ssl_trio():
    with pytest.raises(httpx.ConnectError):
        await bad_ssl()

# Fails with httpcore<1.0.6 and anyio_backend=asyncio
@pytest.mark.parametrize("anyio_backend", ["asyncio", "trio"])
@pytest.mark.anyio
async def test_bad_ssl_anyio():
    with pytest.raises(httpx.ConnectError):
        await bad_ssl()

However, it's not consistent with other HTTP libraries out there:

  • aiohttp raises aiohttp.client_exceptions.ClientSSLError
  • requests raises requests.exceptions.SSLError
  • urllib3 raises urllib3.exceptions.SSLError

As mentioned by Tom in the discussion, httpx should also be raising httpx.SSLError (a new subclass of httpx.RequestError), which would make it easier for downstream users to switch to httpx.

Thanks the neatly summarised issue @pquentin. 😎
Presumably we need to start by resolving this in the underlying httpcore dependency since that doesn't have this distinction. Would you be interested dealing with an issue/PR there. (I can probably take it on otherwise)

Thank you for the offer but I feel that it would be best if you could take it on. 🙏