Skip to content

Commit 3c6d9b7

Browse files
CU-2458: Display server error messages for failed uploads
1 parent 436da91 commit 3c6d9b7

1 file changed

Lines changed: 11 additions & 72 deletions

File tree

cmd/src/code_intel_upload.go

Lines changed: 11 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"github.com/pkg/browser"
1717

18-
"github.com/sourcegraph/sourcegraph/lib/accesstoken"
1918
"github.com/sourcegraph/sourcegraph/lib/codeintel/upload"
2019
"github.com/sourcegraph/sourcegraph/lib/errors"
2120
"github.com/sourcegraph/sourcegraph/lib/output"
@@ -88,7 +87,7 @@ func handleCodeIntelUpload(args []string) error {
8887
}
8988
}
9089
if err != nil {
91-
return handleUploadError(cfg.AccessToken, err)
90+
return handleUploadError(err)
9291
}
9392

9493
client := api.NewClient(api.ClientOpts{
@@ -99,7 +98,7 @@ func handleCodeIntelUpload(args []string) error {
9998
uploadOptions := codeintelUploadOptions(out, isSCIPAvailable)
10099
uploadID, err := upload.UploadIndex(ctx, codeintelUploadFlags.file, client, uploadOptions)
101100
if err != nil {
102-
return handleUploadError(uploadOptions.SourcegraphInstanceOptions.AccessToken, err)
101+
return handleUploadError(err)
103102
}
104103

105104
uploadURL, err := makeCodeIntelUploadURL(uploadID)
@@ -236,10 +235,17 @@ func (e errorWithHint) Error() string {
236235
// given output object is nil then the error will be written to standard out.
237236
//
238237
// This method returns the error that should be passed back up to the runner.
239-
func handleUploadError(accessToken string, err error) error {
238+
func handleUploadError(err error) error {
240239
if errors.Is(err, upload.ErrUnauthorized) {
241-
err = attachHintsForAuthorizationError(accessToken, err)
240+
err = errorWithHint{
241+
err: err,
242+
hint: "For more details, see https://sourcegraph.com/docs/cli/how-tos/creating_an_access_token.",
243+
}
242244
}
245+
// TODO: Add upload.ErrForbidden handling once the lib dependency is updated.
246+
// The server's 403 response body already contains a descriptive error message
247+
// (e.g., "you do not have write permission to this GitHub repository")
248+
// which is displayed to the user via the generic error path.
243249

244250
if codeintelUploadFlags.ignoreUploadFailures {
245251
// Report but don't return the error
@@ -250,76 +256,9 @@ func handleUploadError(accessToken string, err error) error {
250256
return err
251257
}
252258

253-
func attachHintsForAuthorizationError(accessToken string, originalError error) error {
254-
var actionableHints []string
255-
256-
likelyTokenError := accessToken == ""
257-
if _, parseErr := accesstoken.ParsePersonalAccessToken(accessToken); accessToken != "" && parseErr != nil {
258-
likelyTokenError = true
259-
actionableHints = append(actionableHints,
260-
"However, the provided access token does not match expected format; was it truncated?",
261-
"Typically the access token looks like sgp_<40 hex chars> or sgp_<instance-id>_<40 hex chars>.")
262-
}
263-
264-
if likelyTokenError {
265-
return errorWithHint{err: originalError, hint: strings.Join(mergeStringSlices(
266-
[]string{"A Sourcegraph access token must be provided via SRC_ACCESS_TOKEN for uploading SCIP/LSIF data."},
267-
actionableHints,
268-
[]string{"For more details, see https://sourcegraph.com/docs/cli/how-tos/creating_an_access_token."},
269-
), "\n")}
270-
}
271-
272-
needsGitHubToken := strings.HasPrefix(codeintelUploadFlags.repo, "github.com")
273-
needsGitLabToken := strings.HasPrefix(codeintelUploadFlags.repo, "gitlab.com")
274-
275-
if needsGitHubToken {
276-
if codeintelUploadFlags.gitHubToken != "" {
277-
actionableHints = append(actionableHints,
278-
fmt.Sprintf("The supplied -github-token does not indicate that you have collaborator access to %s.", codeintelUploadFlags.repo),
279-
"Please check the value of the supplied token and its permissions on the code host and try again.",
280-
)
281-
} else {
282-
actionableHints = append(actionableHints,
283-
fmt.Sprintf("Please retry your request with a -github-token=XXX with collaborator access to %s.", codeintelUploadFlags.repo),
284-
"This token will be used to check with the code host that the uploading user has write access to the target repository.",
285-
)
286-
}
287-
} else if needsGitLabToken {
288-
if codeintelUploadFlags.gitLabToken != "" {
289-
actionableHints = append(actionableHints,
290-
fmt.Sprintf("The supplied -gitlab-token does not indicate that you have write access to %s.", codeintelUploadFlags.repo),
291-
"Please check the value of the supplied token and its permissions on the code host and try again.",
292-
)
293-
} else {
294-
actionableHints = append(actionableHints,
295-
fmt.Sprintf("Please retry your request with a -gitlab-token=XXX with write access to %s.", codeintelUploadFlags.repo),
296-
"This token will be used to check with the code host that the uploading user has write access to the target repository.",
297-
)
298-
}
299-
} else {
300-
actionableHints = append(actionableHints,
301-
"Verification is supported for the following code hosts: github.com, gitlab.com.",
302-
"Please request support for additional code host verification at https://github.com/sourcegraph/sourcegraph/issues/4967.",
303-
)
304-
}
305-
306-
return errorWithHint{err: originalError, hint: strings.Join(mergeStringSlices(
307-
[]string{"This Sourcegraph instance has enforced auth for SCIP/LSIF uploads."},
308-
actionableHints,
309-
[]string{"For more details, see https://docs.sourcegraph.com/cli/references/code-intel/upload."},
310-
), "\n")}
311-
}
312-
313259
// emergencyOutput creates a default Output object writing to standard out.
314260
func emergencyOutput() *output.Output {
315261
return output.NewOutput(os.Stdout, output.OutputOpts{})
316262
}
317263

318-
func mergeStringSlices(ss ...[]string) []string {
319-
var combined []string
320-
for _, s := range ss {
321-
combined = append(combined, s...)
322-
}
323264

324-
return combined
325-
}

0 commit comments

Comments
 (0)