@@ -57,6 +57,10 @@ func (c *Client) httpClient() *http.Client {
57
57
return http .DefaultClient
58
58
}
59
59
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
+
60
64
// HTTPError is the error type returned when a Gerrit API call does not return
61
65
// the expected status.
62
66
type HTTPError struct {
@@ -69,6 +73,10 @@ func (e *HTTPError) Error() string {
69
73
return fmt .Sprintf ("HTTP status %s on request to %s; %s" , e .Res .Status , e .Res .Request .URL , e .Body )
70
74
}
71
75
76
+ func (e * HTTPError ) Is (target error ) bool {
77
+ return target == ErrResourceNotExist && e .Res .StatusCode == http .StatusNotFound
78
+ }
79
+
72
80
// doArg is an optional argument for the Client.do method.
73
81
type doArg interface {
74
82
isDoArg ()
@@ -463,7 +471,6 @@ func (c *Client) QueryChanges(ctx context.Context, q string, opts ...QueryChange
463
471
464
472
// GetChange returns information about a single change.
465
473
// 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.
467
474
func (c * Client ) GetChange (ctx context.Context , changeID string , opts ... QueryChangesOpt ) (* ChangeInfo , error ) {
468
475
var opt QueryChangesOpt
469
476
switch len (opts ) {
@@ -478,9 +485,6 @@ func (c *Client) GetChange(ctx context.Context, changeID string, opts ...QueryCh
478
485
"n" : condInt (opt .N ),
479
486
"o" : opt .Fields ,
480
487
})
481
- if he , ok := err .(* HTTPError ); ok && he .Res .StatusCode == 404 {
482
- return nil , ErrChangeNotExist
483
- }
484
488
return & change , err
485
489
}
486
490
@@ -774,24 +778,10 @@ func (c *Client) PublishChangeEdit(ctx context.Context, changeID string) error {
774
778
return c .do (ctx , nil , "POST" , "/changes/" + changeID + "/edit:publish" , wantResStatus (http .StatusNoContent ))
775
779
}
776
780
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
-
787
781
// GetProjectInfo returns info about a project.
788
- // If the project doesn't exist, the error will be ErrProjectNotExist.
789
782
func (c * Client ) GetProjectInfo (ctx context.Context , name string ) (ProjectInfo , error ) {
790
783
var res ProjectInfo
791
784
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
- }
795
785
return res , err
796
786
}
797
787
@@ -818,16 +808,12 @@ func (c *Client) GetProjectBranches(ctx context.Context, name string) (map[strin
818
808
return m , nil
819
809
}
820
810
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.
823
812
//
824
813
// See https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-branch.
825
814
func (c * Client ) GetBranch (ctx context.Context , project , branch string ) (BranchInfo , error ) {
826
815
var res BranchInfo
827
816
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
- }
831
817
return res , err
832
818
}
833
819
@@ -896,16 +882,12 @@ func (c *Client) GetProjectTags(ctx context.Context, name string) (map[string]Ta
896
882
return m , nil
897
883
}
898
884
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.
901
886
//
902
887
// See https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-tag.
903
888
func (c * Client ) GetTag (ctx context.Context , project , tag string ) (TagInfo , error ) {
904
889
var res TagInfo
905
890
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
- }
909
891
return res , err
910
892
}
911
893
0 commit comments