-
Notifications
You must be signed in to change notification settings - Fork 0
fix: add friendly Gradata auth error #264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gradata
wants to merge
1
commit into
main
Choose a base branch
from
gra-979-auth-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,8 @@ | |
| from urllib.request import Request, urlopen | ||
|
|
||
| from gradata._http import require_https | ||
| from gradata.brain import AUTH_ERROR_MESSAGE | ||
| from gradata.exceptions import GradataAuthError | ||
|
|
||
| logger = logging.getLogger("gradata.cloud") | ||
|
|
||
|
|
@@ -334,9 +336,11 @@ def _post(self, path: str, data: dict) -> dict[str, Any]: | |
| try: | ||
| with urlopen(req, timeout=30) as resp: | ||
| return json.loads(resp.read().decode("utf-8")) | ||
| except HTTPError as e: | ||
| if e.code == 413: | ||
| raise _TooLargeError() from e | ||
| except HTTPError as e: | ||
| if e.code in (401, 403): | ||
| raise GradataAuthError(AUTH_ERROR_MESSAGE) from e | ||
| if e.code == 413: | ||
| raise _TooLargeError() from e | ||
|
Comment on lines
+339
to
+343
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial | ⚡ Quick win Add a focused unit test for 401/403 → This is a behavior contract change; a deterministic test around mocked 🤖 Prompt for AI Agents |
||
| try: | ||
| body = e.read().decode("utf-8", errors="replace")[:500] | ||
| except Exception: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from gradata import Brain, GradataAuthError | ||
| from gradata.brain import AUTH_ERROR_MESSAGE | ||
|
|
||
|
|
||
| def test_brain_constructor_without_api_key_raises_auth_error(tmp_path, monkeypatch): | ||
| brain_dir = tmp_path / "brain" | ||
| monkeypatch.setenv("HOME", str(tmp_path)) | ||
| monkeypatch.delenv("GRADATA_API_KEY", raising=False) | ||
|
|
||
| Brain.init(brain_dir, name="TestBrain", domain="Testing", embedding="local", interactive=False) | ||
|
|
||
| with pytest.raises(GradataAuthError) as exc: | ||
| Brain(brain_dir) | ||
|
|
||
| assert "GRADATA_API_KEY" in str(exc.value) | ||
| assert str(exc.value) == AUTH_ERROR_MESSAGE | ||
|
|
||
|
|
||
| def test_brain_constructor_accepts_explicit_api_key(tmp_path, monkeypatch): | ||
| brain_dir = tmp_path / "brain" | ||
| monkeypatch.setenv("HOME", str(tmp_path)) | ||
| monkeypatch.delenv("GRADATA_API_KEY", raising=False) | ||
| monkeypatch.setenv("GRADATA_DISABLE_WRITE_THROUGH", "1") | ||
|
|
||
| Brain.init(brain_dir, name="TestBrain", domain="Testing", embedding="local", interactive=False) | ||
|
|
||
| brain = Brain(brain_dir, api_key="gd_test") | ||
| try: | ||
| assert brain._api_key == "gd_test" | ||
| finally: | ||
| brain.close() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial | ⚡ Quick win
Decouple auth message constant from
gradata.brainto reduce module coupling.Importing
AUTH_ERROR_MESSAGEfromgradata.brainmakes the cloud client depend on the full Brain module. Move the shared message constant togradata.exceptions(or a tiny shared auth constants module) and import it from both callers.🤖 Prompt for AI Agents