fix: improve logging for user sync errors in _get_user function#853
Open
D-GopalKrishna wants to merge 2 commits into
Open
fix: improve logging for user sync errors in _get_user function#853D-GopalKrishna wants to merge 2 commits into
D-GopalKrishna wants to merge 2 commits into
Conversation
Contributor
Author
|
@filippomc also fixed the formatting for keycloak.py - which failed the lint check. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes - https://metacell.atlassian.net/browse/NGLASS-1881
Implemented solution
In _get_user() inside cloudharness_django/middleware.py, the outer except Exception handler referenced kc_user.email in the log call. kc_user is only assigned inside inner try blocks, so if an unexpected exception (e.g. a transient DB error) fires before any of those assignments run, Python raises a secondary UnboundLocalError: cannot access local variable 'kc_user', which itself propagates to Sentry.
Replaced kc_user.email with kc_user_id (always in scope at that point) in the log call and removed the stale commented-out line.
How to test this PR
In a test environment, mock User.objects.get to raise a generic Exception (not DoesNotExist/MultipleObjectsReturned) when called inside _get_user
Make an authenticated request (e.g. GET /api/figures/) with a valid Bearer token
Confirm the request returns an anonymous/unauthenticated response (no 500)
Confirm the log output contains "User sync error for kc_id " — not an UnboundLocalError
Confirm no new UnboundLocalError events appear in Sentry
Sanity checks:
Breaking changes (select one):
breaking-changeand the migration procedure is well described abovePossible deployment updates issues (select one):
alert:deploymentTest coverage (select one):
Documentation (select one):
Nice to have (if relevant):