Skip to content

Improved error handling when the backend returns a 401 error, to force a new login #4747

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

Merged
merged 2 commits into from
Jul 29, 2025

Conversation

ifbyol
Copy link
Member

@ifbyol ifbyol commented Jul 29, 2025

Proposed changes

Change the condition to consider when an API error is because of an unauthorized error. We were just handling when the error string returned by the backend was not-authorized but there are other cases like not-found. They key here is that it should be an unauthorized error when the backend returns a 401 error, and that happens when the error string contains: non-200 OK status code: 401 Unauthorized body. That means that the backend returned a 401 error.

If that happens, the function should return the error ErrNotLogged, so when that happens in the context command, it will retrigger a new login flow, regenerating the local token.

As part of this, I did a change to make sure that the flag log-level works also for context commands. It wasn't working because the log level is being changed on the function PersistentPreRun from the main command, but the context command was overriding that function, and by default, cobra only executes the first function in the chain. I set EnableTraverseRunHooks to true, so it executes all the functions in the chain

How to validate

Please provide step-by-step instructions to replicate your validation scenario. For bug fixes, detail how to reproduce both the bug and its fix, along with any observations.

  1. Execute okteto ctx use for one Okteto instance
  2. Go to the UI in that instance, and refresh the login token
  3. If you try to execute any CLI command, you will see how it fails
  4. Now, build the code from this branch, and execute okteto context. You will see how it will say that the token is invalid and it will regenerate one, and it will trigger the login flow

…e a new login

Signed-off-by: Nacho Fuertes <nacho@okteto.com>
@ifbyol ifbyol requested a review from a team as a code owner July 29, 2025 07:35
Signed-off-by: Nacho Fuertes <nacho@okteto.com>
@ifbyol ifbyol added the run-e2e When used on a PR run windows & unix e2e label Jul 29, 2025
Copy link

codecov bot commented Jul 29, 2025

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 48.92%. Comparing base (d7395da) to head (05af87b).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4747   +/-   ##
=======================================
  Coverage   48.91%   48.92%           
=======================================
  Files         356      356           
  Lines       29809    29811    +2     
=======================================
+ Hits        14582    14584    +2     
  Misses      14066    14066           
  Partials     1161     1161           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ifbyol ifbyol merged commit fb91f32 into master Jul 29, 2025
28 of 29 checks passed
@ifbyol ifbyol deleted the ifbyol/improve-error-handling-on-login branch July 29, 2025 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release/bug-fix run-e2e When used on a PR run windows & unix e2e
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants