Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
martingrzzler committed Apr 10, 2024
1 parent cefc0be commit 5e3cc21
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
ErrUnknownOpensearchError = errors.New("opensearch error response could not be parsed as error")
ErrNonJSONError = errors.New("error is not in json format")
ErrUnauthorized = errors.New(http.StatusText(http.StatusUnauthorized))
ErrTooManyRequests = errors.New(http.StatusText(http.StatusTooManyRequests))
)

// Error represents an Opensearch error with only an error field
Expand Down Expand Up @@ -131,10 +132,14 @@ func ParseError(resp *Response) error {
}

if !json.Valid(body) {
if resp.StatusCode == http.StatusUnauthorized {
switch resp.StatusCode {
case http.StatusUnauthorized:
return ErrUnauthorized
case http.StatusTooManyRequests:
return ErrTooManyRequests
default:
return ErrNonJSONError
}
return ErrNonJSONError
}

var testResp struct {
Expand Down
8 changes: 8 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ func TestError(t *testing.T) {
Resp *opensearch.Response
WantedErrors []error
}{
{
Name: "Too many requests",
Resp: &opensearch.Response{
StatusCode: http.StatusTooManyRequests,
Body: io.NopCloser(strings.NewReader(`429 Too Many Requests /testindex/_bulk`)),
},
WantedErrors: []error{opensearch.ErrTooManyRequests},
},
{
Name: "Non JSON error",
Resp: &opensearch.Response{
Expand Down

0 comments on commit 5e3cc21

Please sign in to comment.