Skip to content

Commit 6921eda

Browse files
CU-2458: Display server error messages for failed uploads
1 parent f404049 commit 6921eda

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

cmd/src/code_intel_upload.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,23 @@ func (e errorWithHint) Error() string {
217217
//
218218
// This method returns the error that should be passed back up to the runner.
219219
func handleUploadError(accessToken string, err error) error {
220-
if errors.Is(err, upload.ErrUnauthorized) {
221-
err = attachHintsForAuthorizationError(accessToken, err)
220+
if errors.Is(err, ErrUnauthorized) {
221+
// Extract server detail message if the error was wrapped with one
222+
// (e.g. "must provide github_token" from the upload endpoint).
223+
serverMessage := serverMessageFromError(err)
224+
if serverMessage != "" {
225+
err = errorWithHint{
226+
err: errors.Newf("upload rejected by server: %s", serverMessage),
227+
hint: "For more details, see https://docs.sourcegraph.com/cli/references/code-intel/upload.",
228+
}
229+
} else {
230+
err = attachHintsForAuthorizationError(accessToken, err)
231+
}
232+
} else if strings.Contains(err.Error(), "unexpected status code: 403") {
233+
err = errorWithHint{
234+
err: err,
235+
hint: "For more details, see https://docs.sourcegraph.com/cli/references/code-intel/upload.",
236+
}
222237
}
223238

224239
if codeintelUploadFlags.ignoreUploadFailures {
@@ -230,6 +245,21 @@ func handleUploadError(accessToken string, err error) error {
230245
return err
231246
}
232247

248+
// serverMessageFromError extracts the detail string from a wrapped ErrUnauthorized.
249+
// Returns "" if the error is the bare sentinel (no server message).
250+
func serverMessageFromError(err error) string {
251+
msg := err.Error()
252+
sentinel := ErrUnauthorized.Error()
253+
if msg == sentinel {
254+
return ""
255+
}
256+
// errors.Wrap produces "detail: sentinel", so strip the suffix.
257+
if strings.HasSuffix(msg, ": "+sentinel) {
258+
return strings.TrimSuffix(msg, ": "+sentinel)
259+
}
260+
return ""
261+
}
262+
233263
func attachHintsForAuthorizationError(accessToken string, originalError error) error {
234264
var actionableHints []string
235265

@@ -303,3 +333,5 @@ func mergeStringSlices(ss ...[]string) []string {
303333

304334
return combined
305335
}
336+
337+

cmd/src/code_intel_upload_vendored.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ func performRequest(ctx context.Context, req *http.Request, httpClient upload.Cl
420420
func decodeUploadPayload(resp *http.Response, body []byte, target *int) (bool, error) {
421421
if resp.StatusCode >= 300 {
422422
if resp.StatusCode == http.StatusUnauthorized {
423+
detail := string(bytes.TrimSpace(body))
424+
if detail != "" && !bytes.HasPrefix(bytes.TrimSpace(body), []byte{'<'}) {
425+
return false, errors.Wrap(ErrUnauthorized, detail)
426+
}
423427
return false, ErrUnauthorized
424428
}
425429

0 commit comments

Comments
 (0)