Improved error handling when the backend returns a 401 error, to force a new login #4747
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.
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 likenot-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 forcontext
commands. It wasn't working because the log level is being changed on the functionPersistentPreRun
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 setEnableTraverseRunHooks
totrue
, so it executes all the functions in the chainHow 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.
okteto ctx use
for one Okteto instanceokteto 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