Skip to content

Commit a6e9155

Browse files
committed
report a nicer error when OAuth fails
1 parent 077ecd8 commit a6e9155

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

internal/api/api.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ func (c *client) createHTTPRequest(ctx context.Context, method, p string, body i
183183
} else {
184184
req.Header.Set("User-Agent", "src-cli/"+version.BuildTag)
185185
}
186+
186187
if c.opts.AccessToken != "" {
187188
req.Header.Set("Authorization", "token "+c.opts.AccessToken)
188189
}
@@ -264,10 +265,20 @@ func (r *request) do(ctx context.Context, result any) (bool, error) {
264265
// confirm the status code. You can test this easily with e.g. an invalid
265266
// endpoint like -endpoint=https://google.com
266267
if resp.StatusCode != http.StatusOK {
267-
if resp.StatusCode == http.StatusUnauthorized && isatty.IsCygwinTerminal(os.Stdout.Fd()) {
268-
fmt.Println("You may need to specify or update your access token to use this endpoint.")
269-
fmt.Println("See https://github.com/sourcegraph/src-cli#readme")
270-
fmt.Println("")
268+
if resp.StatusCode == http.StatusUnauthorized {
269+
if oauth.IsOAuthTransport(r.client.httpClient.Transport) {
270+
fmt.Println("The OAuth token is invalid. Please check that the Sourcegraph CLI client is still authorized.")
271+
fmt.Println("")
272+
fmt.Println("To re-authorize, run: src login")
273+
fmt.Println("")
274+
fmt.Println("Learn more at https://github.com/sourcegraph/src-cli#readme")
275+
fmt.Println("")
276+
}
277+
if isatty.IsCygwinTerminal(os.Stdout.Fd()) {
278+
fmt.Println("You may need to specify or update your access token to use this endpoint.")
279+
fmt.Println("See https://github.com/sourcegraph/src-cli#readme")
280+
fmt.Println("")
281+
}
271282
}
272283
body, err := io.ReadAll(resp.Body)
273284
if err != nil {

internal/oauth/http_transport.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,8 @@ func maybeRefresh(ctx context.Context, token *Token) (*Token, error) {
5858
next.ClientID = token.ClientID
5959
return next, nil
6060
}
61+
62+
func IsOAuthTransport(trp http.RoundTripper) bool {
63+
_, ok := trp.(*Transport)
64+
return ok
65+
}

0 commit comments

Comments
 (0)