Skip to content

Commit f00c534

Browse files
committed
internal/frontend: add pathFoundAtLatestError
At the moment when a request for path@version 404s, but other versions of the path exists, we will return an error message letting users know that is the case. Once frontend fetch is live, the message will change to provide users an option to fetch that version of the path. This logic is moved to pathFoundAtLatestError. Updates golang/go#36811 Updates golang/go#37002 Updates golang/go#37106 Change-Id: I1ad15ee13714a68b4b88fd353e43719fda0c0d31 Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/754818 CI-Result: Cloud Build <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
1 parent ccee67b commit f00c534

File tree

3 files changed

+25
-37
lines changed

3 files changed

+25
-37
lines changed

internal/frontend/details.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,20 @@ func pathNotFoundErrorNew(fullPath, version string) error {
196196
},
197197
}
198198
}
199+
200+
// pathFoundAtLatestError returns an error page when the fullPath exists, but
201+
// the version that is requested does not.
202+
func pathFoundAtLatestError(ctx context.Context, pathType, fullPath, version string) error {
203+
if isActiveFrontendFetch(ctx) {
204+
return pathNotFoundErrorNew(fullPath, version)
205+
}
206+
return &serverError{
207+
status: http.StatusNotFound,
208+
epage: &errorPage{
209+
Message: fmt.Sprintf("%s %s@%s is not available.", strings.Title(pathType), fullPath, displayVersion(version, fullPath)),
210+
SecondaryMessage: template.HTML(
211+
fmt.Sprintf(`There are other versions of this %s that are! To view them, `+
212+
`<a href="/%s?tab=versions">click here</a>.`, pathType, fullPath)),
213+
},
214+
}
215+
}

internal/frontend/module.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11-
"html/template"
1211
"net/http"
1312
"strings"
1413

@@ -61,20 +60,12 @@ func (s *Server) serveModulePage(w http.ResponseWriter, r *http.Request, moduleP
6160
return err
6261
}
6362
if requestedVersion != internal.LatestVersion {
64-
if _, err := s.ds.GetModuleInfo(ctx, modulePath, internal.LatestVersion); err != nil {
63+
_, err = s.ds.GetModuleInfo(ctx, modulePath, internal.LatestVersion)
64+
if err == nil {
65+
return pathFoundAtLatestError(ctx, "module", modulePath, displayVersion(requestedVersion, modulePath))
66+
}
67+
if !errors.Is(err, derrors.NotFound) {
6568
log.Errorf(ctx, "error checking for latest module: %v", err)
66-
} else {
67-
return &serverError{
68-
status: http.StatusNotFound,
69-
epage: &errorPage{
70-
Message: fmt.Sprintf("Module %s@%s is not available.",
71-
modulePath, displayVersion(requestedVersion, modulePath)),
72-
SecondaryMessage: template.HTML(
73-
fmt.Sprintf(`There are other versions of this module that are! To view them, `+
74-
`<a href="/mod/%s?tab=versions">click here</a>.</p>`,
75-
modulePath)),
76-
},
77-
}
7869
}
7970
}
8071
return pathNotFoundError(ctx, "module", modulePath, requestedVersion)

internal/frontend/package.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11-
"html/template"
1211
"net/http"
1312
"strings"
1413

@@ -80,16 +79,7 @@ func (s *Server) servePackagePage(w http.ResponseWriter, r *http.Request, pkgPat
8079
}
8180
_, err = s.ds.GetPackage(ctx, pkgPath, modulePath, internal.LatestVersion)
8281
if err == nil {
83-
return &serverError{
84-
status: http.StatusNotFound,
85-
epage: &errorPage{
86-
Message: fmt.Sprintf("Package %s@%s is not available.", pkgPath, displayVersion(version, modulePath)),
87-
SecondaryMessage: template.HTML(
88-
fmt.Sprintf(`There are other versions of this package that are! To view them, `+
89-
`<a href="/%s?tab=versions">click here</a>.`,
90-
pkgPath)),
91-
},
92-
}
82+
return pathFoundAtLatestError(ctx, "package", pkgPath, version)
9383
}
9484
if !errors.Is(err, derrors.NotFound) {
9585
// Unlike the error handling for GetDirectory above, we don't serve an
@@ -184,23 +174,13 @@ func (s *Server) servePackagePageNew(w http.ResponseWriter, r *http.Request, ful
184174
}
185175
// We couldn't find a path at the given version, but if there's one at the latest version
186176
// we can provide a link to it.
187-
modulePath, version, _, err = s.ds.GetPathInfo(ctx, fullPath, inModulePath, internal.LatestVersion)
188-
if err != nil {
177+
if _, _, _, err = s.ds.GetPathInfo(ctx, fullPath, inModulePath, internal.LatestVersion); err != nil {
189178
if errors.Is(err, derrors.NotFound) {
190179
return pathNotFoundError(ctx, "package", fullPath, inVersion)
191180
}
192181
return err
193182
}
194-
return &serverError{
195-
status: http.StatusNotFound,
196-
epage: &errorPage{
197-
Message: fmt.Sprintf("Package %s@%s is not available.", fullPath, displayVersion(version, modulePath)),
198-
SecondaryMessage: template.HTML(
199-
fmt.Sprintf(`There are other versions of this package that are! To view them, `+
200-
`<a href="/%s?tab=versions">click here</a>.`,
201-
fullPath)),
202-
},
203-
}
183+
return pathFoundAtLatestError(ctx, "package", fullPath, inVersion)
204184
}
205185
vdir, err := s.ds.GetDirectoryNew(ctx, fullPath, modulePath, version)
206186
if err != nil {

0 commit comments

Comments
 (0)