Skip to content

Commit f100af7

Browse files
committed
refactor to use a custom exception
1 parent 862c7ca commit f100af7

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/bubble_data_api_client/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ class BubbleError(Exception):
22
"""Base class for all exceptions raised by the library."""
33

44

5+
class ConfigurationError(BubbleError):
6+
"""Raised when required configuration is missing."""
7+
8+
def __init__(self, key: str) -> None:
9+
super().__init__(f"{key} is not configured")
10+
11+
512
class BubbleHttpError(BubbleError):
613
"""Base class for all high level HTTP errors."""
714

src/bubble_data_api_client/pool.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
import asyncio
44
import atexit
55
import threading
6-
from collections.abc import AsyncIterator
6+
from collections.abc import AsyncIterator, Mapping
77
from contextlib import asynccontextmanager
8-
from typing import Any
98

109
import httpx
1110

1211
from .config import get_config
12+
from .exceptions import ConfigurationError
1313
from .transport import httpx_client_factory
1414

1515
# global client pool keyed by config
1616
_clients: dict[tuple[str, str], httpx.AsyncClient] = {}
1717
_lock = threading.Lock()
1818

1919

20-
def _make_client_key(config: dict[str, Any]) -> tuple[str, str]:
20+
def _make_client_key(config: Mapping[str, str | None]) -> tuple[str, str]:
2121
"""Generate a unique key for client pooling based on config."""
2222
base_url = config.get("data_api_root_url") or ""
2323
api_key = config.get("api_key") or ""
@@ -39,10 +39,10 @@ def get_client() -> httpx.AsyncClient:
3939
if key not in _clients:
4040
base_url = config.get("data_api_root_url")
4141
if not base_url:
42-
raise RuntimeError("data_api_root_url")
42+
raise ConfigurationError("data_api_root_url")
4343
api_key = config.get("api_key")
4444
if not api_key:
45-
raise RuntimeError("api_key")
45+
raise ConfigurationError("api_key")
4646
_clients[key] = httpx_client_factory(base_url=base_url, api_key=api_key)
4747
return _clients[key]
4848

src/bubble_data_api_client/transport.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(self) -> None:
3636
pass
3737

3838
async def __aenter__(self) -> typing.Self:
39+
# deferred import to avoid circular dependency: pool imports transport
3940
from .pool import get_client
4041

4142
self._http = get_client()

0 commit comments

Comments
 (0)