Skip to content

Commit 035bec2

Browse files
pilbotgmlewis
authored andcommitted
Consistent errors for GetRef when a Ref doesn't exist (#1207)
Fixes #1208.
1 parent 7ce0678 commit 035bec2

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

github/git_refs.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ func (s *GitService) GetRef(ctx context.Context, owner string, repo string, ref
6868
resp, err := s.client.Do(ctx, req, r)
6969
if _, ok := err.(*json.UnmarshalTypeError); ok {
7070
// Multiple refs, means there wasn't an exact match.
71-
return nil, resp, errors.New("no exact match found for this ref")
71+
return nil, resp, errors.New("multiple matches found for this ref")
72+
} else if resp.StatusCode == 404 {
73+
// No ref, there was no match for the ref
74+
return nil, resp, errors.New("no match found for this ref")
7275
} else if err != nil {
7376
return nil, resp, err
7477
}

github/git_refs_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ func TestGitService_GetRef_singleRef(t *testing.T) {
5656
}
5757
}
5858

59+
func TestGitService_GetRef_noRefs(t *testing.T) {
60+
client, mux, _, teardown := setup()
61+
defer teardown()
62+
63+
mux.HandleFunc("/repos/o/r/git/refs/heads/b", func(w http.ResponseWriter, r *http.Request) {
64+
testMethod(t, r, "GET")
65+
w.WriteHeader(http.StatusNotFound)
66+
fmt.Fprint(w, "[]")
67+
})
68+
69+
_, _, err := client.Git.GetRef(context.Background(), "o", "r", "refs/heads/b")
70+
want := "no match found for this ref"
71+
if err.Error() != want {
72+
t.Errorf("Git.GetRef returned %+v, want %+v", err, want)
73+
}
74+
}
75+
5976
func TestGitService_GetRef_multipleRefs(t *testing.T) {
6077
client, mux, _, teardown := setup()
6178
defer teardown()
@@ -87,7 +104,7 @@ func TestGitService_GetRef_multipleRefs(t *testing.T) {
87104
})
88105

89106
_, _, err := client.Git.GetRef(context.Background(), "o", "r", "refs/heads/b")
90-
want := "no exact match found for this ref"
107+
want := "multiple matches found for this ref"
91108
if err.Error() != want {
92109
t.Errorf("Git.GetRef returned %+v, want %+v", err, want)
93110
}

0 commit comments

Comments
 (0)