Skip to content

Commit 1262032

Browse files
committed
internal/frontend: tweak response text for fetch requests
The response text for different frontend fetch outcomes is updated. The code in fetchRequestStatusAndResponseText is also refactored. For golang/go#36811 For golang/go#37002 Change-Id: I175921acde5edc5cbf677eb1d1f23171b40318b9 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/245640 Run-TryBot: Julie Qiu <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]> TryBot-Result: kokoro <[email protected]>
1 parent b7290b0 commit 1262032

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

internal/frontend/fetch.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ type fetchResult struct {
122122
err error
123123
}
124124

125-
var statusToResponseText = map[int]string{
126-
http.StatusOK: "",
127-
http.StatusRequestTimeout: "We're still working on your request - come back in a few minutes!",
128-
http.StatusInternalServerError: "Something went wrong. We'll keep working on it - try again in a few minutes!",
129-
}
130-
131125
func (s *Server) fetchAndPoll(ctx context.Context, ds internal.DataSource, modulePath, fullPath, requestedVersion string) (status int, responseText string) {
132126
start := time.Now()
133127
defer func() {
@@ -158,7 +152,8 @@ func (s *Server) fetchAndPoll(ctx context.Context, ds internal.DataSource, modul
158152
// finished, return an error letting the user to check back later. The
159153
// worker will still be processing the modules in the background.
160154
if ctx.Err() != nil {
161-
return http.StatusRequestTimeout, statusToResponseText[http.StatusRequestTimeout]
155+
return http.StatusRequestTimeout,
156+
fmt.Sprintf("We're still working on “%s”. Come back in a few minutes!", displayPath(fullPath, requestedVersion))
162157
}
163158
return fetchRequestStatusAndResponseText(results, fullPath, requestedVersion)
164159
}
@@ -224,30 +219,35 @@ func (s *Server) checkPossibleModulePaths(ctx context.Context, db *postgres.DB,
224219
func fetchRequestStatusAndResponseText(results []*fetchResult, fullPath, requestedVersion string) (int, string) {
225220
var moduleMatchingPathPrefix string
226221
for _, fr := range results {
222+
switch fr.status {
227223
// Results are in order of longest module path first. Once an
228-
// appropriate result is found, return. Otherwise, look at the next path.
229-
if fr.status == derrors.ToStatus(derrors.AlternativeModule) {
224+
// appropriate result is found, return. Otherwise, look at the next
225+
// path.
226+
case http.StatusOK:
227+
return fr.status, ""
228+
case http.StatusInternalServerError:
229+
return fr.status, "Oops! Something went wrong."
230+
case derrors.ToStatus(derrors.AlternativeModule):
231+
// TODO(https://golang.org/issue/40306): Make the canonical module
232+
// path a clickable link.
230233
return http.StatusSeeOther,
231-
// TODO(https://golang.org/issue/40306): Make the canonical module path a clickable link.
232-
fmt.Sprintf("“%s” is not a valid path. Were you looking for “%s”?",
234+
fmt.Sprintf("“%s” is not a valid package or module. Were you looking for “%s”?",
233235
displayPath(fullPath, requestedVersion), fr.goModPath)
234236
}
235-
if responseText, ok := statusToResponseText[fr.status]; ok {
236-
return fr.status, responseText
237-
}
237+
238+
// A module was found for a prefix of the path, but the path did not exist
239+
// in that module. Note the longest possible modulePath in this case, and
240+
// let the user know that it exists. For example, if the request was for
241+
// github.com/hashicorp/vault/@master/api, github.com/hashicorp/vault/api
242+
// does not exist at master, but it does in older versions of
243+
// github.com/hashicorp/vault.
238244
if errors.Is(fr.err, errPathDoesNotExistInModule) && moduleMatchingPathPrefix == "" {
239-
// A module was found for a prefix of the path, but the path did
240-
// not exist in that module. Note the longest possible modulePath in
241-
// this case, and let the user know that it exists. For example, if
242-
// the request was for github.com/hashicorp/vault/@master/api,
243-
// github.com/hashicorp/vault/api does not exist at master, but it
244-
// does in older versions of github.com/hashicorp/vault.
245245
moduleMatchingPathPrefix = fr.modulePath
246246
}
247247
}
248248
if moduleMatchingPathPrefix != "" {
249+
// TODO(https://golang.org/issue/40306): Make the link clickable.
249250
return http.StatusNotFound,
250-
// TODO(https://golang.org/issue/40306): Make the link clickable.
251251
fmt.Sprintf("Package “%s” could not be found, but you can view module “%s” at https://pkg.go.dev/mod/%s.",
252252
displayPath(fullPath, requestedVersion),
253253
displayPath(moduleMatchingPathPrefix, requestedVersion),

0 commit comments

Comments
 (0)