Skip to content

Commit

Permalink
Fixes #522 (#523)
Browse files Browse the repository at this point in the history
* Fixes #522

Signed-off-by: Martin Gressler <martin.gressler@code.berlin>

* handle non-json error by returning body

Signed-off-by: Martin Gressler <martin.gressler@code.berlin>

* add to changelog

Signed-off-by: Martin Gressler <martin.gressler@code.berlin>

---------

Signed-off-by: Martin Gressler <martin.gressler@code.berlin>
  • Loading branch information
martingrzzler authored Apr 12, 2024
1 parent 7ea16f7 commit c685fa6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Adjusts tests to new opensearchapi functions and structs ([#421](https://github.com/opensearch-project/opensearch-go/pull/421))
- Changes codecov to comment code coverage to each PR ([#410](https://github.com/opensearch-project/opensearch-go/pull/410))
- Changes module version from v2 to v3 ([#444](https://github.com/opensearch-project/opensearch-go/pull/444))
- Handle unexpected non-json errors with the response body ([#523](https://github.com/opensearch-project/opensearch-go/pull/523))

### Deprecated

Expand Down
8 changes: 1 addition & 7 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"io"
"net/http"
)

// Error vars
Expand All @@ -20,8 +19,6 @@ var (
ErrReadBody = errors.New("failed to read body")
ErrJSONUnmarshalBody = errors.New("failed to json unmarshal body")
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))
)

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

if !json.Valid(body) {
if resp.StatusCode == http.StatusUnauthorized {
return ErrUnauthorized
}
return ErrNonJSONError
return fmt.Errorf("%s", body)
}

var testResp struct {
Expand Down
35 changes: 19 additions & 16 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,6 @@ func TestError(t *testing.T) {
Resp *opensearch.Response
WantedErrors []error
}{
{
Name: "Non JSON error",
Resp: &opensearch.Response{
StatusCode: http.StatusMethodNotAllowed,
Body: io.NopCloser(strings.NewReader(`Test - Trigger an error`)),
},
WantedErrors: []error{opensearch.ErrNonJSONError},
},
{
Name: "error field object",
Resp: &opensearch.Response{
Expand All @@ -205,14 +197,6 @@ func TestError(t *testing.T) {
},
WantedErrors: []error{opensearch.ErrUnknownOpensearchError},
},
{
Name: "unauthorized",
Resp: &opensearch.Response{
StatusCode: http.StatusUnauthorized,
Body: io.NopCloser(strings.NewReader(http.StatusText(http.StatusUnauthorized))),
},
WantedErrors: []error{opensearch.ErrUnauthorized},
},
{
Name: "io read error",
Resp: &opensearch.Response{
Expand Down Expand Up @@ -244,5 +228,24 @@ func TestError(t *testing.T) {
}
})
}

t.Run("unauthorized", func(t *testing.T) {
resp := &opensearch.Response{
StatusCode: http.StatusUnauthorized,
Body: io.NopCloser(strings.NewReader(http.StatusText(http.StatusUnauthorized))),
}
assert.True(t, resp.IsError())
err := opensearch.ParseError(resp)
assert.Equal(t, err.Error(), http.StatusText(http.StatusUnauthorized))
})
t.Run("too many requests", func(t *testing.T) {
resp := &opensearch.Response{
StatusCode: http.StatusTooManyRequests,
Body: io.NopCloser(strings.NewReader("429 Too Many Requests /testindex/_bulk")),
}
assert.True(t, resp.IsError())
err := opensearch.ParseError(resp)
assert.Equal(t, err.Error(), "429 Too Many Requests /testindex/_bulk")
})
})
}

0 comments on commit c685fa6

Please sign in to comment.