Skip to content

Commit c2ca09d

Browse files
committed
gerrit: switch to a generic NotExist error
...and notice that only the one I just added is being used... For golang/go#51797. Change-Id: If62d643c2a0524231e88f1cde2cd4c980e63dd57 Reviewed-on: https://go-review.googlesource.com/c/build/+/411896 Reviewed-by: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Heschi Kreinick <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent de16f12 commit c2ca09d

File tree

3 files changed

+17
-33
lines changed

3 files changed

+17
-33
lines changed

gerrit/gerrit.go

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func (c *Client) httpClient() *http.Client {
5757
return http.DefaultClient
5858
}
5959

60+
// ErrResourceNotExist is returned when the requested resource doesn't exist.
61+
// It is only for use with errors.Is.
62+
var ErrResourceNotExist = errors.New("gerrit: requested resource does not exist")
63+
6064
// HTTPError is the error type returned when a Gerrit API call does not return
6165
// the expected status.
6266
type HTTPError struct {
@@ -69,6 +73,10 @@ func (e *HTTPError) Error() string {
6973
return fmt.Sprintf("HTTP status %s on request to %s; %s", e.Res.Status, e.Res.Request.URL, e.Body)
7074
}
7175

76+
func (e *HTTPError) Is(target error) bool {
77+
return target == ErrResourceNotExist && e.Res.StatusCode == http.StatusNotFound
78+
}
79+
7280
// doArg is an optional argument for the Client.do method.
7381
type doArg interface {
7482
isDoArg()
@@ -463,7 +471,6 @@ func (c *Client) QueryChanges(ctx context.Context, q string, opts ...QueryChange
463471

464472
// GetChange returns information about a single change.
465473
// For the API call, see https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#get-change
466-
// If the change doesn't exist, the error will be ErrChangeNotExist.
467474
func (c *Client) GetChange(ctx context.Context, changeID string, opts ...QueryChangesOpt) (*ChangeInfo, error) {
468475
var opt QueryChangesOpt
469476
switch len(opts) {
@@ -478,9 +485,6 @@ func (c *Client) GetChange(ctx context.Context, changeID string, opts ...QueryCh
478485
"n": condInt(opt.N),
479486
"o": opt.Fields,
480487
})
481-
if he, ok := err.(*HTTPError); ok && he.Res.StatusCode == 404 {
482-
return nil, ErrChangeNotExist
483-
}
484488
return &change, err
485489
}
486490

@@ -774,24 +778,10 @@ func (c *Client) PublishChangeEdit(ctx context.Context, changeID string) error {
774778
return c.do(ctx, nil, "POST", "/changes/"+changeID+"/edit:publish", wantResStatus(http.StatusNoContent))
775779
}
776780

777-
// ErrXNotExist is returned when the requested X doesn't exist.
778-
// It is not necessarily returned unless a method is documented as
779-
// returning it.
780-
var (
781-
ErrProjectNotExist = errors.New("gerrit: requested project does not exist")
782-
ErrChangeNotExist = errors.New("gerrit: requested change does not exist")
783-
ErrTagNotExist = errors.New("gerrit: requested tag does not exist")
784-
ErrBranchNotExist = errors.New("gerrit: requested branch does not exist")
785-
)
786-
787781
// GetProjectInfo returns info about a project.
788-
// If the project doesn't exist, the error will be ErrProjectNotExist.
789782
func (c *Client) GetProjectInfo(ctx context.Context, name string) (ProjectInfo, error) {
790783
var res ProjectInfo
791784
err := c.do(ctx, &res, "GET", fmt.Sprintf("/projects/%s", name))
792-
if he, ok := err.(*HTTPError); ok && he.Res.StatusCode == 404 {
793-
return res, ErrProjectNotExist
794-
}
795785
return res, err
796786
}
797787

@@ -818,16 +808,12 @@ func (c *Client) GetProjectBranches(ctx context.Context, name string) (map[strin
818808
return m, nil
819809
}
820810

821-
// GetBranch gets a particular branch in project. If the branch doesn't exist, the
822-
// error will be ErrBranchNotExist.
811+
// GetBranch gets a particular branch in project.
823812
//
824813
// See https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-branch.
825814
func (c *Client) GetBranch(ctx context.Context, project, branch string) (BranchInfo, error) {
826815
var res BranchInfo
827816
err := c.do(ctx, &res, "GET", fmt.Sprintf("/projects/%s/branches/%s", project, branch))
828-
if he, ok := err.(*HTTPError); ok && he.Res.StatusCode == 404 {
829-
return BranchInfo{}, ErrBranchNotExist
830-
}
831817
return res, err
832818
}
833819

@@ -896,16 +882,12 @@ func (c *Client) GetProjectTags(ctx context.Context, name string) (map[string]Ta
896882
return m, nil
897883
}
898884

899-
// GetTag returns a particular tag on project. If the tag doesn't exist, the
900-
// error will be ErrTagNotExist.
885+
// GetTag returns a particular tag on project.
901886
//
902887
// See https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-tag.
903888
func (c *Client) GetTag(ctx context.Context, project, tag string) (TagInfo, error) {
904889
var res TagInfo
905890
err := c.do(ctx, &res, "GET", fmt.Sprintf("/projects/%s/tags/%s", project, tag))
906-
if he, ok := err.(*HTTPError); ok && he.Res.StatusCode == 404 {
907-
return TagInfo{}, ErrTagNotExist
908-
}
909891
return res, err
910892
}
911893

gerrit/gerrit_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package gerrit
66

77
import (
88
"context"
9+
"errors"
910
"io"
1011
"net/http"
1112
"net/http/httptest"
@@ -69,8 +70,8 @@ func TestProjectNotFound(t *testing.T) {
6970
defer s.Close()
7071
c := NewClient(s.URL, NoAuth)
7172
_, err := c.GetProjectInfo(context.Background(), "unknown")
72-
if err != ErrProjectNotExist {
73-
t.Errorf("expected to get ErrProjectNotExist, got %v", err)
73+
if !errors.Is(err, ErrResourceNotExist) {
74+
t.Errorf("expected to get ErrResourceNotExist, got %v", err)
7475
}
7576
}
7677

@@ -176,8 +177,8 @@ func TestGetChangeError(t *testing.T) {
176177
if !hitServer {
177178
t.Errorf("expected to hit test server, didn't")
178179
}
179-
if err != ErrChangeNotExist {
180-
t.Errorf("expected ErrChangeNotExist, got %v", err)
180+
if !errors.Is(err, ErrResourceNotExist) {
181+
t.Errorf("expected ErrResourceNotExist, got %v", err)
181182
}
182183
}
183184

internal/task/gerrit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package task
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"strings"
78
"time"
@@ -93,7 +94,7 @@ func (c *RealGerritClient) AwaitSubmit(ctx context.Context, changeID, parentComm
9394

9495
func (c *RealGerritClient) Tag(ctx context.Context, project, tag, commit string) error {
9596
info, err := c.Client.GetTag(ctx, project, tag)
96-
if err != nil && err != gerrit.ErrTagNotExist {
97+
if err != nil && !errors.Is(err, gerrit.ErrResourceNotExist) {
9798
return fmt.Errorf("checking if tag already exists: %v", err)
9899
}
99100
if err == nil {

0 commit comments

Comments
 (0)