Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI: fix error handling for invalid access tokens (#5427)
Fixes Cody-3261. Fixes CODY-2967. Fixes CODY-2909. Previously, Cody CLI users got a poor experience if they had provided expired credentials with `SRC_ACCESS_TOKEN`. The `cody chat` command reported that the user was "not logged in" and `cody auth login` confusingly reported that the user was already logged in. Now, Cody CLI reports a helpful error message saying the user has invalid credentials and documents how to fix the problem by creating a new access token. This fix ended up being harder to implement than I original expected because the code related to authentication was badly organized. Specifically, we didn't distinguish between error cases (invalid token) and unauthenticate cases (the user hasn't logged in yet). I used this issue as an opportunity to clean up the code so that all the validation is done in a single function that handles all cases. ## Test plan Manually tested the following flows - `cody chat` works with environment variable login and secret storage login - `export SRC_ACCESS_TOKEN=foobar`, and confirm that `cody chat` and `cody auth login` give helpful error messages. Also confirm this happens when the secret storage has an invalid token (manually edited keychain item) ``` ❯ export SRC_ACCESS_TOKEN=foobar ❯ pnpm agent auth login ... ✖ The provided access token is invalid. The most common cause for this is that the access token has expired. If you are using SRC_ACCESS_TOKEN, create a new token at https://sourcegraph.sourcegraph.com/user/settings/tokens/new?description=CodyCLI and update the value of SRC_ACCESS_TOKEN. If you are using `cody auth login --web`, run `cody auth logout` and try logging in again. ``` - Confirm that `cody auth logout` reports a helpful error message when `SRC_ACCESS_TOKEN` is set ``` ❯ pnpm agent auth logout ... ✖ You cannot logout when using SRC_ACCESS_TOKEN with logout. To fix this problem, run `unset SRC_ACCESS_TOKEN` and try again. ``` <!-- Required. See https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles. --> ## Changelog * Cody CLI now reports a helpful error message when authenticating with an invalid access token <!-- OPTIONAL; info at https://www.notion.so/sourcegraph/Writing-a-changelog-entry-dd997f411d524caabf0d8d38a24a878c -->
- Loading branch information