From 4cf1f6e05f83e6c81ae3a745197d2190203fd20c Mon Sep 17 00:00:00 2001 From: William Cooke Date: Mon, 24 Jun 2019 12:38:15 +0100 Subject: [PATCH 1/2] Consistent errors for GetRef when a Ref doesn't exist --- github/git_refs.go | 3 +++ github/git_refs_test.go | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/github/git_refs.go b/github/git_refs.go index 3f381d5f2b6..7cc02ece66f 100644 --- a/github/git_refs.go +++ b/github/git_refs.go @@ -69,6 +69,9 @@ func (s *GitService) GetRef(ctx context.Context, owner string, repo string, ref if _, ok := err.(*json.UnmarshalTypeError); ok { // Multiple refs, means there wasn't an exact match. return nil, resp, errors.New("no exact match found for this ref") + } else if resp.StatusCode == 404 { + // No ref, there was no match for the ref + return nil, resp, errors.New("no exact match found for this ref") } else if err != nil { return nil, resp, err } diff --git a/github/git_refs_test.go b/github/git_refs_test.go index 3cae3c085fd..0b19b4d1f4f 100644 --- a/github/git_refs_test.go +++ b/github/git_refs_test.go @@ -56,6 +56,23 @@ func TestGitService_GetRef_singleRef(t *testing.T) { } } +func TestGitService_GetRef_noRefs(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/git/refs/heads/b", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.WriteHeader(http.StatusNotFound) + fmt.Fprint(w, "[]") + }) + + _, _, err := client.Git.GetRef(context.Background(), "o", "r", "refs/heads/b") + want := "no exact match found for this ref" + if err.Error() != want { + t.Errorf("Git.GetRef returned %+v, want %+v", err, want) + } +} + func TestGitService_GetRef_multipleRefs(t *testing.T) { client, mux, _, teardown := setup() defer teardown() From 05bba03c3a518990e6bef80ad3cf2347b2cfa375 Mon Sep 17 00:00:00 2001 From: William Cooke Date: Mon, 1 Jul 2019 10:15:36 +0100 Subject: [PATCH 2/2] :ok_hand: Use differing errors for each case --- github/git_refs.go | 4 ++-- github/git_refs_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/github/git_refs.go b/github/git_refs.go index 7cc02ece66f..fe16880e7b0 100644 --- a/github/git_refs.go +++ b/github/git_refs.go @@ -68,10 +68,10 @@ func (s *GitService) GetRef(ctx context.Context, owner string, repo string, ref resp, err := s.client.Do(ctx, req, r) if _, ok := err.(*json.UnmarshalTypeError); ok { // Multiple refs, means there wasn't an exact match. - return nil, resp, errors.New("no exact match found for this ref") + return nil, resp, errors.New("multiple matches found for this ref") } else if resp.StatusCode == 404 { // No ref, there was no match for the ref - return nil, resp, errors.New("no exact match found for this ref") + return nil, resp, errors.New("no match found for this ref") } else if err != nil { return nil, resp, err } diff --git a/github/git_refs_test.go b/github/git_refs_test.go index 0b19b4d1f4f..6c8a1d87951 100644 --- a/github/git_refs_test.go +++ b/github/git_refs_test.go @@ -67,7 +67,7 @@ func TestGitService_GetRef_noRefs(t *testing.T) { }) _, _, err := client.Git.GetRef(context.Background(), "o", "r", "refs/heads/b") - want := "no exact match found for this ref" + want := "no match found for this ref" if err.Error() != want { t.Errorf("Git.GetRef returned %+v, want %+v", err, want) } @@ -104,7 +104,7 @@ func TestGitService_GetRef_multipleRefs(t *testing.T) { }) _, _, err := client.Git.GetRef(context.Background(), "o", "r", "refs/heads/b") - want := "no exact match found for this ref" + want := "multiple matches found for this ref" if err.Error() != want { t.Errorf("Git.GetRef returned %+v, want %+v", err, want) }