Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 4088a1b

Browse files
authored
Merge pull request #561 from spenczar/wrap_deduce_errs
Include more error context in deduction failures
2 parents 2022f99 + 16880dd commit 4088a1b

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

internal/gps/deduce.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package gps
66

77
import (
88
"context"
9-
"errors"
109
"fmt"
1110
"io"
1211
"net/http"
@@ -18,6 +17,7 @@ import (
1817
"sync"
1918

2019
radix "github.com/armon/go-radix"
20+
"github.com/pkg/errors"
2121
)
2222

2323
var (
@@ -707,6 +707,7 @@ func (hmd *httpMetadataDeducer) deduce(ctx context.Context, path string) (pathDe
707707
opath := path
708708
u, path, err := normalizeURI(path)
709709
if err != nil {
710+
err = errors.Wrapf(err, "unable to normalize URI")
710711
hmd.deduceErr = err
711712
return
712713
}
@@ -716,11 +717,15 @@ func (hmd *httpMetadataDeducer) deduce(ctx context.Context, path string) (pathDe
716717
// Make the HTTP call to attempt to retrieve go-get metadata
717718
var root, vcs, reporoot string
718719
err = hmd.suprvsr.do(ctx, path, ctHTTPMetadata, func(ctx context.Context) error {
719-
root, vcs, reporoot, err = parseMetadata(ctx, path, u.Scheme)
720+
root, vcs, reporoot, err = getMetadata(ctx, path, u.Scheme)
721+
if err != nil {
722+
err = errors.Wrapf(err, "unable to read metadata")
723+
}
720724
return err
721725
})
722726
if err != nil {
723-
hmd.deduceErr = fmt.Errorf("unable to deduce repository and source type for: %q", opath)
727+
err = errors.Wrapf(err, "unable to deduce repository and source type for %q", opath)
728+
hmd.deduceErr = err
724729
return
725730
}
726731
pd.root = root
@@ -729,7 +734,8 @@ func (hmd *httpMetadataDeducer) deduce(ctx context.Context, path string) (pathDe
729734
// the real URL to hit
730735
repoURL, err := url.Parse(reporoot)
731736
if err != nil {
732-
hmd.deduceErr = fmt.Errorf("server returned bad URL in go-get metadata: %q", reporoot)
737+
err = errors.Wrapf(err, "server returned bad URL in go-get metadata, reporoot=%q", reporoot)
738+
hmd.deduceErr = err
733739
return
734740
}
735741

@@ -742,7 +748,7 @@ func (hmd *httpMetadataDeducer) deduce(ctx context.Context, path string) (pathDe
742748
// To err on the secure side, do NOT allow the same in the other
743749
// direction (https -> http).
744750
if u.Scheme != "http" || repoURL.Scheme != "https" {
745-
hmd.deduceErr = fmt.Errorf("scheme mismatch for %q: input asked for %q, but go-get metadata specified %q", path, u.Scheme, repoURL.Scheme)
751+
hmd.deduceErr = errors.Errorf("scheme mismatch for %q: input asked for %q, but go-get metadata specified %q", path, u.Scheme, repoURL.Scheme)
746752
return
747753
}
748754
}
@@ -755,7 +761,7 @@ func (hmd *httpMetadataDeducer) deduce(ctx context.Context, path string) (pathDe
755761
case "hg":
756762
pd.mb = maybeHgSource{url: repoURL}
757763
default:
758-
hmd.deduceErr = fmt.Errorf("unsupported vcs type %s in go-get metadata from %s", vcs, path)
764+
hmd.deduceErr = errors.Errorf("unsupported vcs type %s in go-get metadata from %s", vcs, path)
759765
return
760766
}
761767

@@ -792,7 +798,7 @@ func normalizeURI(p string) (u *url.URL, newpath string, err error) {
792798
} else {
793799
u, err = url.Parse(p)
794800
if err != nil {
795-
return nil, "", fmt.Errorf("%q is not a valid URI", p)
801+
return nil, "", errors.Errorf("%q is not a valid URI", p)
796802
}
797803
}
798804

@@ -805,20 +811,14 @@ func normalizeURI(p string) (u *url.URL, newpath string, err error) {
805811
}
806812

807813
if !pathvld.MatchString(newpath) {
808-
return nil, "", fmt.Errorf("%q is not a valid import path", newpath)
814+
return nil, "", errors.Errorf("%q is not a valid import path", newpath)
809815
}
810816

811817
return
812818
}
813819

814820
// fetchMetadata fetches the remote metadata for path.
815821
func fetchMetadata(ctx context.Context, path, scheme string) (rc io.ReadCloser, err error) {
816-
defer func() {
817-
if err != nil {
818-
err = fmt.Errorf("unable to determine remote metadata protocol: %s", err)
819-
}
820-
}()
821-
822822
if scheme == "http" {
823823
rc, err = doFetchMetadata(ctx, "http", path)
824824
return
@@ -839,48 +839,48 @@ func doFetchMetadata(ctx context.Context, scheme, path string) (io.ReadCloser, e
839839
case "https", "http":
840840
req, err := http.NewRequest("GET", url, nil)
841841
if err != nil {
842-
return nil, fmt.Errorf("failed to access url %q", url)
842+
return nil, errors.Wrapf(err, "unable to build HTTP request for URL %q", url)
843843
}
844844

845845
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
846846
if err != nil {
847-
return nil, fmt.Errorf("failed to access url %q", url)
847+
return nil, errors.Wrapf(err, "failed HTTP request to URL %q", url)
848848
}
849849

850850
return resp.Body, nil
851851
default:
852-
return nil, fmt.Errorf("unknown remote protocol scheme: %q", scheme)
852+
return nil, errors.Errorf("unknown remote protocol scheme: %q", scheme)
853853
}
854854
}
855855

856-
// parseMetadata fetches and decodes remote metadata for path.
856+
// getMetadata fetches and decodes remote metadata for path.
857857
//
858858
// scheme is optional. If it's http, only http will be attempted for fetching.
859859
// Any other scheme (including none) will first try https, then fall back to
860860
// http.
861-
func parseMetadata(ctx context.Context, path, scheme string) (string, string, string, error) {
861+
func getMetadata(ctx context.Context, path, scheme string) (string, string, string, error) {
862862
rc, err := fetchMetadata(ctx, path, scheme)
863863
if err != nil {
864-
return "", "", "", err
864+
return "", "", "", errors.Wrapf(err, "unable to fetch raw metadata")
865865
}
866866
defer rc.Close()
867867

868868
imports, err := parseMetaGoImports(rc)
869869
if err != nil {
870-
return "", "", "", err
870+
return "", "", "", errors.Wrapf(err, "unable to parse go-import metadata")
871871
}
872872
match := -1
873873
for i, im := range imports {
874874
if !strings.HasPrefix(path, im.Prefix) {
875875
continue
876876
}
877877
if match != -1 {
878-
return "", "", "", fmt.Errorf("multiple meta tags match import path %q", path)
878+
return "", "", "", errors.Errorf("multiple meta tags match import path %q", path)
879879
}
880880
match = i
881881
}
882882
if match == -1 {
883-
return "", "", "", fmt.Errorf("go-import metadata not found")
883+
return "", "", "", errors.Errorf("go-import metadata not found")
884884
}
885885
return imports[match].Prefix, imports[match].VCS, imports[match].RepoRoot, nil
886886
}

0 commit comments

Comments
 (0)