-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
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 -->
- vscode-v1.70.3
- vscode-v1.70.2
- vscode-v1.70.1
- vscode-v1.70.0
- vscode-v1.68.0
- vscode-v1.66.1
- vscode-v1.66.0
- vscode-v1.64.0
- vscode-v1.62.1
- vscode-v1.62.0
- vscode-v1.60.0
- vscode-v1.58.0
- vscode-v1.56.0
- vscode-v1.54.0
- vscode-v1.52.0
- vscode-v1.50.0
- vscode-v1.48.1
- vscode-v1.48.0
- vscode-v1.46.0
- vscode-v1.44.0
- vscode-v1.42.0
- vscode-v1.40.2
- vscode-v1.40.1
- vscode-v1.40.0
- vscode-v1.38.3
- vscode-v1.38.2
- vscode-v1.38.1
- vscode-v1.38.0
- vscode-v1.36.3
- vscode-v1.36.2
- vscode-v1.36.1
- vscode-v1.36.0
- vscode-v1.34.3
- vscode-v1.34.2
- vscode-v1.34.1
- vscode-v1.34.0
- vscode-insiders-v1.71.1740529642
- vscode-insiders-v1.71.1740433710
- vscode-insiders-v1.71.1740356887
- vscode-insiders-v1.71.1740336433
- vscode-insiders-v1.71.1740270486
- vscode-insiders-v1.71.1740097642
- vscode-insiders-v1.71.1740011243
- vscode-insiders-v1.71.1739924839
- vscode-insiders-v1.71.1739884527
- vscode-insiders-v1.71.1739843482
- vscode-insiders-v1.69.1739838422
- vscode-insiders-v1.69.1739811381
- vscode-insiders-v1.69.1739412747
- vscode-insiders-v1.67.1739404418
- vscode-insiders-v1.67.1739376765
- vscode-insiders-v1.67.1738976389
- vscode-insiders-v1.67.1738821693
- vscode-insiders-v1.65.1738206769
- vscode-insiders-v1.63.1738151334
- vscode-insiders-v1.63.1738082771
- vscode-insiders-v1.63.1737990305
- vscode-insiders-v1.63.1737975128
- vscode-insiders-v1.63.1737728242
- vscode-insiders-v1.63.1737564634
- vscode-insiders-v1.63.1737542054
- vscode-insiders-v1.63.1737539166
- vscode-insiders-v1.61.1737039427
- vscode-insiders-v1.61.1737037795
- vscode-insiders-v1.61.1736988085
- vscode-insiders-v1.59.1736740193
- vscode-insiders-v1.59.1736468968
- vscode-insiders-v1.59.1736418931
- vscode-insiders-v1.57.1735928494
- vscode-insiders-v1.55.1735238533
- vscode-insiders-v1.53.1734972437
- vscode-insiders-v1.53.1734584341
- vscode-insiders-v1.51.1734469056
- vscode-insiders-v1.51.1734458469
- vscode-insiders-v1.51.1734457160
- vscode-insiders-v1.51.1734412096
- vscode-insiders-v1.51.1734386478
- vscode-insiders-v1.51.1734117319
- vscode-insiders-v1.51.1734045774
- vscode-insiders-v1.51.1733963807
- vscode-insiders-v1.49.1733881527
- vscode-insiders-v1.49.1733854084
- vscode-insiders-v1.49.1733815015
- vscode-insiders-v1.49.1733492458
- vscode-insiders-v1.49.1733379007
- vscode-insiders-v1.47.1732805697
- vscode-insiders-v1.47.1732692277
- vscode-insiders-v1.45.1732320290
- vscode-insiders-v1.45.1732175052
- vscode-insiders-v1.43.1732076023
- vscode-insiders-v1.43.1731571491
- vscode-insiders-v1.41.1731027960
- vscode-insiders-v1.41.1730971227
- vscode-insiders-v1.41.1730930945
- v1.34.1
- jb-v7.71.3-nightly
- jb-v7.71.2-nightly
- jb-v7.71.1-nightly
- jb-v7.71.0-nightly
- jb-v7.70.3
- jb-v7.70.2
- jb-v7.70.1
- jb-v7.70.0
- jb-v7.69.1-nightly
- jb-v7.69.0-nightly
- jb-v7.68.1
- jb-v7.68.0
- jb-v7.67.1-nightly
- jb-v7.67.0-nightly
- jb-v7.66.0
- jb-v7.65.2-nightly
- jb-v7.65.1-nightly
- jb-v7.65.0-nightly
- jb-v7.12.6
- jb-v7.12.6-nightly
- jb-v7.12.5-nightly
- jb-v7.12.4-nightly
- jb-v7.12.3-nightly
- jb-v7.12.2-nightly
- jb-v7.12.1-nightly
- jb-v7.12.0-nightly
- jb-v7.11.2
- jb-v7.11.1
- jb-v7.11.1-nightly
- jb-v7.11.0-nightly
- jb-v7.11.x-nightly
- jb-v7.10.1
- jb-v7.10.1-nightly
- jb-v7.10.0-nightly
- jb-v7.9.1
- jb-v7.9.0-nightly
- jb-v7.8.0
- jb-v7.8.0-nightly
- jb-v7.7.1
- jb-v7.7.1-nightly
- jb-v7.7.0-nightly
- jb-v7.6.3
- jb-v7.6.3-nightly
- jb-v7.6.2-nightly
- jb-v7.6.1-nightly
- jb-v7.6.0-nightly
- jb-v7.5.6
- jb-v7.5.6-nightly
- jb-v7.5.5-nightly
- jb-v7.5.4-nightly
- jb-v7.5.3-nightly
- jb-v7.5.2-nightly
- jb-v7.5.1-nightly
- jb-v7.5.0-nightly
- agent-v5.5.15
- agent-v5.5.14
- 876a2a38
Showing
13 changed files
with
292 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 73 additions & 69 deletions
142
agent/recordings/cody-chat_103640681/recording.har.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,32 @@ | ||
import type { Ora } from 'ora' | ||
import type { AuthenticationOptions } from './command-login' | ||
|
||
export function notLoggedIn(spinner: Ora): void { | ||
if (!spinner.isSpinning) { | ||
return | ||
} | ||
spinner.fail('Not logged in. To fix this problem, run:\n\tcody auth login --web') | ||
} | ||
|
||
export function errorSpinner(spinner: Ora, error: Error, options: AuthenticationOptions): void { | ||
if (error.message.includes('Invalid access token')) { | ||
const createNewTokenURL = options.endpoint + '/user/settings/tokens/new?description=CodyCLI' | ||
spinner.fail( | ||
'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 ${createNewTokenURL} 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. ' | ||
) | ||
} else { | ||
spinner.suffixText = error.stack ?? '' | ||
spinner.fail(error.message) | ||
} | ||
} | ||
|
||
export function unknownErrorSpinner(spinner: Ora, error: unknown, options: AuthenticationOptions): void { | ||
if (error instanceof Error) { | ||
errorSpinner(spinner, error, options) | ||
} else { | ||
spinner.fail(String(error)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters