Skip to content

Commit 9d7bd4d

Browse files
committed
Fetch should return a unique error type when ref not found
It can be useful for callers to distinguish between an error of "couldn't find remote ref" and some other error like "network error". Creating an explicit error type for this allows consumers to determine the kind of error using the errors.Is and errors.As interface added in go1.13
1 parent 6da5e59 commit 9d7bd4d

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

remote.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ var (
3232
ErrExactSHA1NotSupported = errors.New("server does not support exact SHA1 refspec")
3333
)
3434

35+
type NoMatchingRefSpecError struct {
36+
refSpec config.RefSpec
37+
}
38+
39+
func (e NoMatchingRefSpecError) Error() string {
40+
return fmt.Sprintf("couldn't find remote ref %q", e.refSpec.Src())
41+
}
42+
43+
func (e NoMatchingRefSpecError) Is(target error) bool {
44+
_, ok := target.(NoMatchingRefSpecError)
45+
return ok
46+
}
47+
3548
const (
3649
// This describes the maximum number of commits to walk when
3750
// computing the haves to send to a server, for each ref in the
@@ -751,7 +764,7 @@ func doCalculateRefs(
751764
})
752765

753766
if !matched && !s.IsWildcard() {
754-
return fmt.Errorf("couldn't find remote ref %q", s.Src())
767+
return NoMatchingRefSpecError{refSpec: s}
755768
}
756769

757770
return err

remote_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package git
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"io"
78
"io/ioutil"
89
"os"
@@ -145,6 +146,7 @@ func (s *RemoteSuite) TestFetchNonExistantReference(c *C) {
145146
})
146147

147148
c.Assert(err, ErrorMatches, "couldn't find remote ref.*")
149+
c.Assert(errors.Is(err, NoMatchingRefSpecError{}), Equals, true)
148150
}
149151

150152
func (s *RemoteSuite) TestFetchContext(c *C) {

0 commit comments

Comments
 (0)