Skip to content

fix: improve logging for user sync errors in _get_user function#853

Open
D-GopalKrishna wants to merge 2 commits into
developfrom
fix/unbound-kc-user-middleware-exception
Open

fix: improve logging for user sync errors in _get_user function#853
D-GopalKrishna wants to merge 2 commits into
developfrom
fix/unbound-kc-user-middleware-exception

Conversation

@D-GopalKrishna
Copy link
Copy Markdown
Contributor

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:

  • The pull request is explicitly linked to the relevant issue(s)
  • The issue is well described: clearly states the problem and the general proposed solution(s)
  • In this PR it is explicitly stated how to test the current change
  • The labels in the issue set the scope and the type of issue (bug, feature, etc.)
  • The relevant components are indicated in the issue (if any)
  • All the automated test checks are passing
  • All the linked issues are included in one Sprint
  • All the linked issues are in the Review state
  • All the linked issues are assigned

Breaking changes (select one):

  • The present changes do not change the preexisting api in any way
  • This PR and the issue are tagged as a breaking-change and the migration procedure is well described above

Possible deployment updates issues (select one):

  • There is no reason why deployments based on CloudHarness may break after the current update
  • This PR and the issue are tagged as alert:deployment

Test coverage (select one):

  • Tests for the relevant cases are included in this pr
  • The changes included in this pr are out of the current test coverage scope

Documentation (select one):

  • The documentation has been updated to match the current changes
  • The changes included in this PR are out of the current documentation scope

Nice to have (if relevant):

  • Screenshots of the changes
  • Explanatory video/animated gif

@D-GopalKrishna
Copy link
Copy Markdown
Contributor Author

@filippomc also fixed the formatting for keycloak.py - which failed the lint check.

@D-GopalKrishna D-GopalKrishna requested a review from filippomc May 14, 2026 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant