]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: clarify error for invalid proxy responses
authorMichał Łowicki <mlowicki@gmail.com>
Mon, 11 May 2020 21:57:26 +0000 (22:57 +0100)
committerJay Conrod <jayconrod@google.com>
Fri, 21 Aug 2020 15:46:15 +0000 (15:46 +0000)
Add information that error comes from parsing module proxy responses.

Fixes #38680

Change-Id: Ic318b9cdbca789c1b0d983e083e692a914892fbd
Reviewed-on: https://go-review.googlesource.com/c/go/+/233437
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/modfetch/proxy.go
src/cmd/go/proxy_test.go
src/cmd/go/testdata/script/mod_proxy_invalid.txt [new file with mode: 0644]
src/cmd/go/testdata/script/mod_query_empty.txt

index 1c35d0b99bd2be203d0735d0a88764fb7856f782..4ac26650a9b94100db9188a94f4948f98819cc9a 100644 (file)
@@ -242,8 +242,9 @@ func TryProxies(f func(proxy string) error) error {
 }
 
 type proxyRepo struct {
-       url  *url.URL
-       path string
+       url         *url.URL
+       path        string
+       redactedURL string
 }
 
 func newProxyRepo(baseURL, path string) (Repo, error) {
@@ -268,10 +269,10 @@ func newProxyRepo(baseURL, path string) (Repo, error) {
        if err != nil {
                return nil, err
        }
-
+       redactedURL := base.Redacted()
        base.Path = strings.TrimSuffix(base.Path, "/") + "/" + enc
        base.RawPath = strings.TrimSuffix(base.RawPath, "/") + "/" + pathEscape(enc)
-       return &proxyRepo{base, path}, nil
+       return &proxyRepo{base, path, redactedURL}, nil
 }
 
 func (p *proxyRepo) ModulePath() string {
@@ -413,7 +414,7 @@ func (p *proxyRepo) Stat(rev string) (*RevInfo, error) {
        }
        info := new(RevInfo)
        if err := json.Unmarshal(data, info); err != nil {
-               return nil, p.versionError(rev, err)
+               return nil, p.versionError(rev, fmt.Errorf("invalid response from proxy %q: %w", p.redactedURL, err))
        }
        if info.Version != rev && rev == module.CanonicalVersion(rev) && module.Check(p.path, rev) == nil {
                // If we request a correct, appropriate version for the module path, the
@@ -434,7 +435,7 @@ func (p *proxyRepo) Latest() (*RevInfo, error) {
        }
        info := new(RevInfo)
        if err := json.Unmarshal(data, info); err != nil {
-               return nil, p.versionError("", err)
+               return nil, p.versionError("", fmt.Errorf("invalid response from proxy %q: %w", p.redactedURL, err))
        }
        return info, nil
 }
index 2a4d2935b3853011b4d967cce519df364188799d..7f58fb8ce43f4dbd0c17909706cb0cef0a2b7615 100644 (file)
@@ -131,6 +131,12 @@ func proxyHandler(w http.ResponseWriter, r *http.Request) {
        }
        path := r.URL.Path[len("/mod/"):]
 
+       // /mod/invalid returns faulty responses.
+       if strings.HasPrefix(path, "invalid/") {
+               w.Write([]byte("invalid"))
+               return
+       }
+
        // /mod/quiet/ does not print errors.
        quiet := false
        if strings.HasPrefix(path, "quiet/") {
diff --git a/src/cmd/go/testdata/script/mod_proxy_invalid.txt b/src/cmd/go/testdata/script/mod_proxy_invalid.txt
new file mode 100644 (file)
index 0000000..6427cc1
--- /dev/null
@@ -0,0 +1,8 @@
+env GO111MODULE=on
+env GOPROXY=$GOPROXY/invalid
+
+! go list -m rsc.io/quote@latest
+stderr '^go list -m: module rsc.io/quote: invalid response from proxy "'$GOPROXY'": invalid character ''i'' looking for beginning of value$'
+
+! go list -m rsc.io/quote@1.5.2
+stderr '^go list -m: rsc.io/quote@1.5.2: invalid version: invalid response from proxy "'$GOPROXY'": invalid character ''i'' looking for beginning of value$'
index 4d8259b40f1a4be6efe05ab0fca35008be71b77d..b3ea3e3de0a5ac638012c0d51ef7fd01f166695d 100644 (file)
@@ -40,7 +40,7 @@ env GOPROXY=file:///$WORK/gatekeeper
 chmod 0000 $WORK/gatekeeper/example.com/join/subpkg/@latest
 cp go.mod.orig go.mod
 ! go get -d example.com/join/subpkg
-stderr 'go get example.com/join/subpkg: module example.com/join/subpkg: (invalid character .+|reading file://.*/gatekeeper/example.com/join/subpkg/@latest: .+)'
+stderr 'go get example.com/join/subpkg: module example.com/join/subpkg: (invalid response from proxy ".+": invalid character .+|reading file://.*/gatekeeper/example.com/join/subpkg/@latest: .+)'
 
 -- go.mod.orig --
 module example.com/othermodule