Skip to content

Commit

Permalink
internal/frontend: add pathFoundAtLatestError
Browse files Browse the repository at this point in the history
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 <devtools-proctor-result-processor@system.gserviceaccount.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
  • Loading branch information
julieqiu committed Jun 4, 2020
1 parent ccee67b commit f00c534
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
17 changes: 17 additions & 0 deletions internal/frontend/details.go
Expand Up @@ -196,3 +196,20 @@ func pathNotFoundErrorNew(fullPath, version string) error {
},
}
}

// pathFoundAtLatestError returns an error page when the fullPath exists, but
// the version that is requested does not.
func pathFoundAtLatestError(ctx context.Context, pathType, fullPath, version string) error {
if isActiveFrontendFetch(ctx) {
return pathNotFoundErrorNew(fullPath, version)
}
return &serverError{
status: http.StatusNotFound,
epage: &errorPage{
Message: fmt.Sprintf("%s %s@%s is not available.", strings.Title(pathType), fullPath, displayVersion(version, fullPath)),
SecondaryMessage: template.HTML(
fmt.Sprintf(`There are other versions of this %s that are! To view them, `+
`<a href="/%s?tab=versions">click here</a>.`, pathType, fullPath)),
},
}
}
19 changes: 5 additions & 14 deletions internal/frontend/module.go
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"errors"
"fmt"
"html/template"
"net/http"
"strings"

Expand Down Expand Up @@ -61,20 +60,12 @@ func (s *Server) serveModulePage(w http.ResponseWriter, r *http.Request, moduleP
return err
}
if requestedVersion != internal.LatestVersion {
if _, err := s.ds.GetModuleInfo(ctx, modulePath, internal.LatestVersion); err != nil {
_, err = s.ds.GetModuleInfo(ctx, modulePath, internal.LatestVersion)
if err == nil {
return pathFoundAtLatestError(ctx, "module", modulePath, displayVersion(requestedVersion, modulePath))
}
if !errors.Is(err, derrors.NotFound) {
log.Errorf(ctx, "error checking for latest module: %v", err)
} else {
return &serverError{
status: http.StatusNotFound,
epage: &errorPage{
Message: fmt.Sprintf("Module %s@%s is not available.",
modulePath, displayVersion(requestedVersion, modulePath)),
SecondaryMessage: template.HTML(
fmt.Sprintf(`There are other versions of this module that are! To view them, `+
`<a href="/mod/%s?tab=versions">click here</a>.</p>`,
modulePath)),
},
}
}
}
return pathNotFoundError(ctx, "module", modulePath, requestedVersion)
Expand Down
26 changes: 3 additions & 23 deletions internal/frontend/package.go
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"errors"
"fmt"
"html/template"
"net/http"
"strings"

Expand Down Expand Up @@ -80,16 +79,7 @@ func (s *Server) servePackagePage(w http.ResponseWriter, r *http.Request, pkgPat
}
_, err = s.ds.GetPackage(ctx, pkgPath, modulePath, internal.LatestVersion)
if err == nil {
return &serverError{
status: http.StatusNotFound,
epage: &errorPage{
Message: fmt.Sprintf("Package %s@%s is not available.", pkgPath, displayVersion(version, modulePath)),
SecondaryMessage: template.HTML(
fmt.Sprintf(`There are other versions of this package that are! To view them, `+
`<a href="/%s?tab=versions">click here</a>.`,
pkgPath)),
},
}
return pathFoundAtLatestError(ctx, "package", pkgPath, version)
}
if !errors.Is(err, derrors.NotFound) {
// Unlike the error handling for GetDirectory above, we don't serve an
Expand Down Expand Up @@ -184,23 +174,13 @@ func (s *Server) servePackagePageNew(w http.ResponseWriter, r *http.Request, ful
}
// We couldn't find a path at the given version, but if there's one at the latest version
// we can provide a link to it.
modulePath, version, _, err = s.ds.GetPathInfo(ctx, fullPath, inModulePath, internal.LatestVersion)
if err != nil {
if _, _, _, err = s.ds.GetPathInfo(ctx, fullPath, inModulePath, internal.LatestVersion); err != nil {
if errors.Is(err, derrors.NotFound) {
return pathNotFoundError(ctx, "package", fullPath, inVersion)
}
return err
}
return &serverError{
status: http.StatusNotFound,
epage: &errorPage{
Message: fmt.Sprintf("Package %s@%s is not available.", fullPath, displayVersion(version, modulePath)),
SecondaryMessage: template.HTML(
fmt.Sprintf(`There are other versions of this package that are! To view them, `+
`<a href="/%s?tab=versions">click here</a>.`,
fullPath)),
},
}
return pathFoundAtLatestError(ctx, "package", fullPath, inVersion)
}
vdir, err := s.ds.GetDirectoryNew(ctx, fullPath, modulePath, version)
if err != nil {
Expand Down

0 comments on commit f00c534

Please sign in to comment.