internal/frontend: return bad request errors

Errors from candidateModulePaths are now returned in
servePathNotFoundPage, which will all be bad request errors.

Previously, if a request was made to an predetermined invalid path, the
user would only get a BadRequest error after clicking "Request <path>".

The error is now returned on page load.

Change-Id: I98b25122c678ab2a59b70939cab8b74e2b627a2a
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/245902
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
Julie Qiu 2020-07-30 15:48:45 -04:00
Родитель 03d34d43ff
Коммит ffac86ebe0
3 изменённых файлов: 13 добавлений и 1 удалений

Просмотреть файл

@ -488,7 +488,7 @@ func (s *Server) servePathNotFoundPage(w http.ResponseWriter, r *http.Request, d
}
modulePaths, err := candidateModulePaths(fullPath)
if err != nil {
return pathNotFoundError(ctx, pathType, fullPath, requestedVersion)
return err
}
results := s.checkPossibleModulePaths(ctx, db, fullPath, requestedVersion, modulePaths, false)
for _, fr := range results {

Просмотреть файл

@ -469,6 +469,12 @@ func candidateModulePaths(fullPath string) (_ []string, err error) {
modulePaths = append([]string{path}, modulePaths...)
path += "/"
}
if len(modulePaths) == 0 {
return nil, &serverError{
status: http.StatusBadRequest,
err: fmt.Errorf("invalid path: %q", fullPath),
}
}
return modulePaths, nil
}

Просмотреть файл

@ -881,6 +881,12 @@ func frontendFetchTestCases() []serverTestCase {
in("h3.Fetch-message.js-fetchMessage", text("example.com/unknown"))),
requiredExperiments: experiment.NewSet(internal.ExperimentFrontendFetch),
},
{
name: "bad request, invalid github module path, frontend experiment",
urlPath: "/github.com/foo",
wantStatusCode: http.StatusBadRequest,
requiredExperiments: experiment.NewSet(internal.ExperimentFrontendFetch),
},
}
}