Skip to content

Reduce Faraday::UnauthorizedError errors#748

Open
zetter-rpf wants to merge 2 commits intomainfrom
reduce-auth-errors
Open

Reduce Faraday::UnauthorizedError errors#748
zetter-rpf wants to merge 2 commits intomainfrom
reduce-auth-errors

Conversation

@zetter-rpf
Copy link
Contributor

@zetter-rpf zetter-rpf commented Mar 25, 2026

Status

What's changed?

  • Only try try to fetch the user once if token is invalid
  • Don't log unauthorised errors in sentry

See commits for more

Steps to perform after deploying to production

  • Resolve issue in sentry

@cla-bot cla-bot bot added the cla-signed label Mar 25, 2026
@github-actions
Copy link

github-actions bot commented Mar 25, 2026

Test coverage

89.86% line coverage reported by SimpleCov.
Run: https://github.com/RaspberryPiFoundation/editor-api/actions/runs/23545795991

I noticed that if a token is invalid because it has expired, we might call `from_token` multiple times. This is because `@current_user` is set to `nil` so won't be cached.

`from_token` calls `HydraPublicApiClient.fetch_oauth_user` which does an API request. Since we might call current_user many times in a controller, we shouldn't be doing a API request from each one.

Since we're currently recording unauthorized errors in Sentry, this often resulted in 3-5 errors being recorded for the same request.

Instead, load the current user once. This might mean in some API calls we're loading the user when we don't need to, but since almost all API actions should validate the current user I don't see this as a problem.

The identifible by cookie file doesn't have this problem, but I've updated it for consistency.
Sentry should be for exceptional errors. Having this reported to sentry was creating a lot of noise (~20k events a day) and using up a large proportion of our Sentry limits. If in the future we want to track this, we should instead log it or track unauthorized responses as a proxy.

Before doing this I did some testing to see if the tokens were long lasting and are automatically refresh - as far as I can see they are and wasn't able to reproduce any issues by leaving tabs open or by offloading tabs. This would point to most tokens being invalid due to expiry. This could be due to the refresh token not working in inactive tabs.
@zetter-rpf zetter-rpf marked this pull request as ready for review March 25, 2026 14:22
Copilot AI review requested due to automatic review settings March 25, 2026 14:22
Copy link
Contributor

Copilot AI left a 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 aims to reduce authentication-related noise by (1) preventing repeated user lookups when a token is invalid, and (2) avoiding Sentry reporting for expected 401s from Hydra.

Changes:

  • Stop reporting Faraday::UnauthorizedError (401) from HydraPublicApiClient#fetch_oauth_user to Sentry.
  • Change controller authentication concerns to load current_user via a before_action instead of @current_user ||= ... memoization.
  • Update User.from_token spec to reflect the new unauthorized behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
spec/models/user_spec.rb Removes Sentry expectation for invalid-token path; keeps nil-return behavior check.
lib/hydra_public_api_client.rb Stops capturing 401 Unauthorized exceptions to Sentry and returns nil.
app/controllers/concerns/identifiable_by_cookie.rb Switches cookie-based identification to a before_action + attr_reader :current_user.
app/controllers/concerns/identifiable.rb Switches header-based identification to a before_action + attr_reader :current_user.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@zetter-rpf zetter-rpf changed the title Reduce auth errors Reduce Faraday::UnauthorizedError errors Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants