Skip to content

Commit 7f51e4e

Browse files
authored
Merge pull request #91 from WorkflowAI/guillaume/no-api-key-provided
Handle no api key provided error separately
2 parents 89ece3b + 27a9bc6 commit 7f51e4e

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

workflowai/core/client/_api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pydantic import BaseModel, TypeAdapter, ValidationError
77

88
from workflowai.core._logger import logger
9-
from workflowai.core.domain.errors import BaseError, WorkflowAIError
9+
from workflowai.core.domain.errors import BaseError, InvalidAPIKeyError, WorkflowAIError
1010

1111
# A type for return values
1212
_R = TypeVar("_R")
@@ -26,6 +26,11 @@ def _get_url(self, run: bool = False):
2626

2727
@asynccontextmanager
2828
async def _client(self, run: bool = False):
29+
if not self.api_key:
30+
raise InvalidAPIKeyError(
31+
response=None,
32+
error=BaseError(message="No API key provided", code="invalid_api_key"),
33+
)
2934
source_headers = self.source_headers or {}
3035
async with httpx.AsyncClient(
3136
base_url=self._get_url(run),

workflowai/core/client/_api_test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pytest_httpx import HTTPXMock, IteratorStream
77

88
from workflowai.core.client._api import APIClient
9-
from workflowai.core.domain.errors import WorkflowAIError
9+
from workflowai.core.domain.errors import InvalidAPIKeyError, WorkflowAIError
1010

1111

1212
@pytest.fixture
@@ -136,3 +136,16 @@ async def test_stream(self, httpx_mock: HTTPXMock, client: APIClient, exception:
136136
pass
137137

138138
assert e.value.error.code == "connection_error"
139+
140+
async def test_empty_api_key(self, client: APIClient):
141+
"""Check that we return a pretty error when there is no api key provided"""
142+
client.api_key = ""
143+
144+
# no need to add any response, httpx will complain that the header is illegal
145+
with pytest.raises(InvalidAPIKeyError) as e:
146+
await client.get(
147+
path="test_path",
148+
returns=_TestOutputModel,
149+
)
150+
assert e.value.error.code == "invalid_api_key"
151+
assert e.value.message.startswith("❌ No API key provided")

workflowai/core/domain/errors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,9 @@ class InvalidAPIKeyError(WorkflowAIError):
188188
@property
189189
@override
190190
def message(self) -> str:
191+
base_message = (self.status_code is None and "No API key provided") or "Your API key is invalid"
191192
return (
192-
"❌ Your API key is invalid. Please double-check your API key, "
193+
f"❌ {base_message}. Please double-check your API key, "
193194
"or create a new one at https://workflowai.com/organization/settings/api-keys "
194195
"or from your self-hosted WorkflowAI instance."
195196
)

0 commit comments

Comments
 (0)