Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes opensearch-project/opensearch-go#522 #523

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,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")
})
})
}
Loading