Skip to content

Commit 58cb5d3

Browse files
Normalize config env validation to HyperbrowserError
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 7255a1f commit 58cb5d3

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

hyperbrowser/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from dataclasses import dataclass
22
import os
33

4+
from .exceptions import HyperbrowserError
5+
46

57
@dataclass
68
class ClientConfig:
@@ -13,7 +15,9 @@ class ClientConfig:
1315
def from_env(cls) -> "ClientConfig":
1416
api_key = os.environ.get("HYPERBROWSER_API_KEY")
1517
if api_key is None:
16-
raise ValueError("HYPERBROWSER_API_KEY environment variable is required")
18+
raise HyperbrowserError(
19+
"HYPERBROWSER_API_KEY environment variable is required"
20+
)
1721

1822
base_url = os.environ.get(
1923
"HYPERBROWSER_BASE_URL", "https://api.hyperbrowser.ai"

tests/test_config.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
3+
from hyperbrowser.config import ClientConfig
4+
from hyperbrowser.exceptions import HyperbrowserError
5+
6+
7+
def test_client_config_from_env_raises_hyperbrowser_error_without_api_key(monkeypatch):
8+
monkeypatch.delenv("HYPERBROWSER_API_KEY", raising=False)
9+
10+
with pytest.raises(HyperbrowserError, match="HYPERBROWSER_API_KEY"):
11+
ClientConfig.from_env()
12+
13+
14+
def test_client_config_from_env_reads_api_key_and_base_url(monkeypatch):
15+
monkeypatch.setenv("HYPERBROWSER_API_KEY", "test-key")
16+
monkeypatch.setenv("HYPERBROWSER_BASE_URL", "https://example.local")
17+
18+
config = ClientConfig.from_env()
19+
20+
assert config.api_key == "test-key"
21+
assert config.base_url == "https://example.local"

0 commit comments

Comments
 (0)