-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: Handle login failures with clear error messages #11629
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
base: master
Are you sure you want to change the base?
fix: Handle login failures with clear error messages #11629
Conversation
for more information, see https://pre-commit.ci
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.
Pull request overview
This PR improves error handling for authentication failures in the Open Library API client by making login failures explicit rather than silent, preventing downstream 403 errors when operations are attempted with unauthenticated sessions.
Key changes:
- Modified
login()method inapi.pyto return boolean success/failure instead of silently swallowing authentication errors - Added login result validation and clear error messages in
copydocs.pyto guide users when authentication fails - Enhanced error messages provide actionable guidance for both development and production scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| openlibrary/api.py | Modified login() to return True on success, False on auth failures (401/403), and re-raise non-auth errors; added comprehensive docstring documenting the new return value and exception behavior |
| scripts/copydocs.py | Added login result checking for both autologin() and login() calls with clear error messages and actionable guidance; exits gracefully with status code 1 on authentication failures; includes helpful instructions for setting up credentials |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def login(self, username, password): | ||
| """Login to Open Library with given credentials.""" | ||
| """Login to Open Library with given credentials. | ||
| Returns: | ||
| bool: True if login was successful, False otherwise. | ||
| Raises: | ||
| OLError: If a non-authentication HTTP error occurs. | ||
| """ | ||
| headers = {'Content-Type': 'application/json'} | ||
| try: | ||
| data = json.dumps({"username": username, "password": password}) | ||
| response = self._request( | ||
| '/account/login', method='POST', data=data, headers=headers | ||
| ) | ||
| except OLError as e: | ||
| response = e | ||
| if e.code in (401, 403): | ||
| return False | ||
| raise | ||
|
|
||
| if 'Set-Cookie' in response.headers: | ||
| cookies = response.headers['Set-Cookie'].split(',') | ||
| self.cookie = ';'.join([c.split(';')[0] for c in cookies]) | ||
| return True | ||
| logger.warning("Login request succeeded but no session cookie was received") | ||
| return False |
Copilot
AI
Dec 26, 2025
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.
The modified login() method that now returns a boolean value lacks test coverage. Tests should be added to verify the behavior when login succeeds (returns True), when authentication fails with 401/403 (returns False), and when other HTTP errors occur (re-raises OLError). The PR description mentions tests in openlibrary/tests/test_api.py, but this file was not included in the pull request.
|
PTAL @cdrini |
Closes #11628
Technical
The
login()method inapi.pywas silently swallowing authentication failures, causingcopydocs.pyto proceed with unauthenticated requests that returned 403 errors.Changes:
api.py: Modifiedlogin()to returnTrue/Falseinstead of silently failing. Re-raises non-auth errors.copydocs.py: Now checks login result and exits with clear error messages on failureTesting
copydocs.pywith invalid credentials - should exit with helpful error messageStakeholders
@cdrini