@@ -6,7 +6,6 @@ package gps
6
6
7
7
import (
8
8
"context"
9
- "errors"
10
9
"fmt"
11
10
"io"
12
11
"net/http"
@@ -18,6 +17,7 @@ import (
18
17
"sync"
19
18
20
19
radix "github.com/armon/go-radix"
20
+ "github.com/pkg/errors"
21
21
)
22
22
23
23
var (
@@ -707,6 +707,7 @@ func (hmd *httpMetadataDeducer) deduce(ctx context.Context, path string) (pathDe
707
707
opath := path
708
708
u , path , err := normalizeURI (path )
709
709
if err != nil {
710
+ err = errors .Wrapf (err , "unable to normalize URI" )
710
711
hmd .deduceErr = err
711
712
return
712
713
}
@@ -716,11 +717,15 @@ func (hmd *httpMetadataDeducer) deduce(ctx context.Context, path string) (pathDe
716
717
// Make the HTTP call to attempt to retrieve go-get metadata
717
718
var root , vcs , reporoot string
718
719
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
+ }
720
724
return err
721
725
})
722
726
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
724
729
return
725
730
}
726
731
pd .root = root
@@ -729,7 +734,8 @@ func (hmd *httpMetadataDeducer) deduce(ctx context.Context, path string) (pathDe
729
734
// the real URL to hit
730
735
repoURL , err := url .Parse (reporoot )
731
736
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
733
739
return
734
740
}
735
741
@@ -742,7 +748,7 @@ func (hmd *httpMetadataDeducer) deduce(ctx context.Context, path string) (pathDe
742
748
// To err on the secure side, do NOT allow the same in the other
743
749
// direction (https -> http).
744
750
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 )
746
752
return
747
753
}
748
754
}
@@ -755,7 +761,7 @@ func (hmd *httpMetadataDeducer) deduce(ctx context.Context, path string) (pathDe
755
761
case "hg" :
756
762
pd .mb = maybeHgSource {url : repoURL }
757
763
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 )
759
765
return
760
766
}
761
767
@@ -792,7 +798,7 @@ func normalizeURI(p string) (u *url.URL, newpath string, err error) {
792
798
} else {
793
799
u , err = url .Parse (p )
794
800
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 )
796
802
}
797
803
}
798
804
@@ -805,20 +811,14 @@ func normalizeURI(p string) (u *url.URL, newpath string, err error) {
805
811
}
806
812
807
813
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 )
809
815
}
810
816
811
817
return
812
818
}
813
819
814
820
// fetchMetadata fetches the remote metadata for path.
815
821
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
-
822
822
if scheme == "http" {
823
823
rc , err = doFetchMetadata (ctx , "http" , path )
824
824
return
@@ -839,48 +839,48 @@ func doFetchMetadata(ctx context.Context, scheme, path string) (io.ReadCloser, e
839
839
case "https" , "http" :
840
840
req , err := http .NewRequest ("GET" , url , nil )
841
841
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 )
843
843
}
844
844
845
845
resp , err := http .DefaultClient .Do (req .WithContext (ctx ))
846
846
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 )
848
848
}
849
849
850
850
return resp .Body , nil
851
851
default :
852
- return nil , fmt .Errorf ("unknown remote protocol scheme: %q" , scheme )
852
+ return nil , errors .Errorf ("unknown remote protocol scheme: %q" , scheme )
853
853
}
854
854
}
855
855
856
- // parseMetadata fetches and decodes remote metadata for path.
856
+ // getMetadata fetches and decodes remote metadata for path.
857
857
//
858
858
// scheme is optional. If it's http, only http will be attempted for fetching.
859
859
// Any other scheme (including none) will first try https, then fall back to
860
860
// 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 ) {
862
862
rc , err := fetchMetadata (ctx , path , scheme )
863
863
if err != nil {
864
- return "" , "" , "" , err
864
+ return "" , "" , "" , errors . Wrapf ( err , "unable to fetch raw metadata" )
865
865
}
866
866
defer rc .Close ()
867
867
868
868
imports , err := parseMetaGoImports (rc )
869
869
if err != nil {
870
- return "" , "" , "" , err
870
+ return "" , "" , "" , errors . Wrapf ( err , "unable to parse go-import metadata" )
871
871
}
872
872
match := - 1
873
873
for i , im := range imports {
874
874
if ! strings .HasPrefix (path , im .Prefix ) {
875
875
continue
876
876
}
877
877
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 )
879
879
}
880
880
match = i
881
881
}
882
882
if match == - 1 {
883
- return "" , "" , "" , fmt .Errorf ("go-import metadata not found" )
883
+ return "" , "" , "" , errors .Errorf ("go-import metadata not found" )
884
884
}
885
885
return imports [match ].Prefix , imports [match ].VCS , imports [match ].RepoRoot , nil
886
886
}
0 commit comments