Skip to content

Commit

Permalink
Add IsConflict and IsStatusCode error checkers
Browse files Browse the repository at this point in the history
Added an `IsConflict` func to check against HTTP status 409. Refactored
IsNotFound, IsTimeout and IsConflict to use a generic IsStatusCode.
  • Loading branch information
olivere committed Jul 18, 2017
1 parent 5b4cd4e commit 4c8608a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 27 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Bruce Zhou [@brucez-isell](https://github.com/brucez-isell)
cforbes [@cforbes](https://github.com/cforbes)
Chris M [@tebriel](https://github.com/tebriel)
Christophe Courtaut [@kri5](https://github.com/kri5)
Connor Peet [@connor4312](https://github.com/connor4312)
Conrad Pankoff [@deoxxa](https://github.com/deoxxa)
Corey Scott [@corsc](https://github.com/corsc)
Daniel Barrett [@shendaras](https://github.com/shendaras)
Expand Down
41 changes: 14 additions & 27 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,51 +93,38 @@ func (e *Error) Error() string {
// returned HTTP status 404. The err parameter can be of type *elastic.Error,
// elastic.Error, *http.Response or int (indicating the HTTP status code).
func IsNotFound(err interface{}) bool {
switch e := err.(type) {
case *http.Response:
return e.StatusCode == http.StatusNotFound
case *Error:
return e.Status == http.StatusNotFound
case Error:
return e.Status == http.StatusNotFound
case int:
return e == http.StatusNotFound
}
return false
return IsStatusCode(err, http.StatusNotFound)
}

// IsTimeout returns true if the given error indicates that Elasticsearch
// returned HTTP status 408. The err parameter can be of type *elastic.Error,
// elastic.Error, *http.Response or int (indicating the HTTP status code).
func IsTimeout(err interface{}) bool {
switch e := err.(type) {
case *http.Response:
return e.StatusCode == http.StatusRequestTimeout
case *Error:
return e.Status == http.StatusRequestTimeout
case Error:
return e.Status == http.StatusRequestTimeout
case int:
return e == http.StatusRequestTimeout
}
return false
return IsStatusCode(err, http.StatusRequestTimeout)
}

// IsConflict returns true if the given error indicates that the ElasticSearch
// IsConflict returns true if the given error indicates that the Elasticsearch
// operation resulted in a version conflict. This can occur in operations like
// `update` or `index` with `op_type=create`. The err parameter can be of
// type *elastic.Error, elastic.Error, *http.Response or int (indicating the
// HTTP status code).
func IsConflict(err interface{}) bool {
return IsStatusCode(err, http.StatusConflict)
}

// IsStatusCode returns true if the given error indicates that the Elasticsearch
// operation returned the specified HTTP status code. The err parameter can be of
// type *http.Response, *Error, Error, or int (indicating the HTTP status code).
func IsStatusCode(err interface{}, code int) bool {
switch e := err.(type) {
case *http.Response:
return e.StatusCode == http.StatusConflict
return e.StatusCode == code
case *Error:
return e.Status == http.StatusConflict
return e.Status == code
case Error:
return e.Status == http.StatusConflict
return e.Status == code
case int:
return e == http.StatusConflict
return e == code
}
return false
}
Expand Down
93 changes: 93 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,96 @@ func TestIsTimeout(t *testing.T) {
t.Errorf("expected %v; got: %v", want, got)
}
}

func TestIsConflict(t *testing.T) {
if got, want := IsConflict(nil), false; got != want {
t.Errorf("expected %v; got: %v", want, got)
}
if got, want := IsConflict(""), false; got != want {
t.Errorf("expected %v; got: %v", want, got)
}
if got, want := IsConflict(200), false; got != want {
t.Errorf("expected %v; got: %v", want, got)
}
if got, want := IsConflict(http.StatusConflict), true; got != want {
t.Errorf("expected %v; got: %v", want, got)
}

if got, want := IsConflict(&Error{Status: 409}), true; got != want {
t.Errorf("expected %v; got: %v", want, got)
}
if got, want := IsConflict(&Error{Status: 200}), false; got != want {
t.Errorf("expected %v; got: %v", want, got)
}

if got, want := IsConflict(Error{Status: 409}), true; got != want {
t.Errorf("expected %v; got: %v", want, got)
}
if got, want := IsConflict(Error{Status: 200}), false; got != want {
t.Errorf("expected %v; got: %v", want, got)
}

if got, want := IsConflict(&http.Response{StatusCode: 409}), true; got != want {
t.Errorf("expected %v; got: %v", want, got)
}
if got, want := IsConflict(&http.Response{StatusCode: 200}), false; got != want {
t.Errorf("expected %v; got: %v", want, got)
}
}

func TestIsStatusCode(t *testing.T) {
tests := []struct {
Error interface{}
Code int
Want bool
}{
// #0
{
Error: nil,
Code: 200,
Want: false,
},
// #1
{
Error: "",
Code: 200,
Want: false,
},
// #2
{
Error: http.StatusConflict,
Code: 409,
Want: true,
},
// #3
{
Error: http.StatusConflict,
Code: http.StatusInternalServerError,
Want: false,
},
// #4
{
Error: &Error{Status: http.StatusConflict},
Code: 409,
Want: true,
},
// #5
{
Error: Error{Status: http.StatusConflict},
Code: 409,
Want: true,
},
// #6
{
Error: &http.Response{StatusCode: http.StatusConflict},
Code: 409,
Want: true,
},
}

for i, tt := range tests {
if have, want := IsStatusCode(tt.Error, tt.Code), tt.Want; have != want {
t.Errorf("#%d: have %v, want %v", i, have, want)
}
}
}

0 comments on commit 4c8608a

Please sign in to comment.