Skip to content

Commit

Permalink
error: adjust ParseError function to handle non json errors with a cu…
Browse files Browse the repository at this point in the history
…stom error

Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
  • Loading branch information
Jakob3xD committed Apr 7, 2024
1 parent 54dd638 commit dd0c6d8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Adds the `Explanation` field containing the document explain details to the `SearchHit` struct. ([#504](https://github.com/opensearch-project/opensearch-go/pull/504))
- Adds the `Fields` field containing the document fields to the `SearchHit` struct. ([#508](https://github.com/opensearch-project/opensearch-go/pull/508))
- Adds new error types ([#512](https://github.com/opensearch-project/opensearch-go/pull/506))
- Adds handling of non json errors to ParseError ([#512](https://github.com/opensearch-project/opensearch-go/pull/506))

### Changed
- Use docker compose v2 instead of v1 ([#506](https://github.com/opensearch-project/opensearch-go/pull/506))
Expand Down
8 changes: 8 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ func ParseError(resp *Response) error {
if err != nil {
return fmt.Errorf("%w: %w", ErrReadBody, err)
}

if !json.Valid(body) {
if resp.StatusCode == http.StatusUnauthorized {
return ErrUnauthorized
}
return ErrNonJSONError
}

var testResp struct {
Status any `json:"status"`
Error any `json:"error"`
Expand Down
50 changes: 26 additions & 24 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ func TestError(t *testing.T) {
resp := &opensearch.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(
strings.NewReader(`
{
strings.NewReader(`{
"error":{
"root_cause":[{
"type":"resource_already_exists_exception",
Expand Down Expand Up @@ -68,8 +67,7 @@ func TestError(t *testing.T) {
t.Run("Unmarshal errors", func(t *testing.T) {
t.Run("dummy", func(t *testing.T) {
reader := io.NopCloser(
strings.NewReader(`
{
strings.NewReader(`{
"status": "400"
}`),
)
Expand All @@ -85,8 +83,7 @@ func TestError(t *testing.T) {
})
t.Run("string", func(t *testing.T) {
reader := io.NopCloser(
strings.NewReader(`
{
strings.NewReader(`{
"error": 0,
"status": 500
}`),
Expand All @@ -108,8 +105,7 @@ func TestError(t *testing.T) {
resp := &opensearch.Response{
StatusCode: http.StatusMethodNotAllowed,
Body: io.NopCloser(
strings.NewReader(`
{
strings.NewReader(`{
"error": "Incorrect HTTP method for uri [/_doc] and method [POST], allowed: [HEAD, DELETE, PUT, GET]",
"status":405
}`),
Expand All @@ -128,8 +124,7 @@ func TestError(t *testing.T) {
resp := &opensearch.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(
strings.NewReader(`
{
strings.NewReader(`{
"error": "no handler found for uri [/_plugins/_security/xxx] and method [GET]"
}`),
),
Expand All @@ -146,8 +141,7 @@ func TestError(t *testing.T) {
resp := &opensearch.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(
strings.NewReader(`
{
strings.NewReader(`{
"status": "error",
"reason": "Invalid configuration",
"invalid_keys": {
Expand Down Expand Up @@ -188,20 +182,12 @@ func TestError(t *testing.T) {
WantedErrors []error
}{
{
Name: "response for StringError",
Name: "Non JSON error",
Resp: &opensearch.Response{
StatusCode: http.StatusMethodNotAllowed,
Body: io.NopCloser(strings.NewReader(`"Test - Trigger an error"`)),
Body: io.NopCloser(strings.NewReader(`Test - Trigger an error`)),
},
WantedErrors: []error{opensearch.ErrJSONUnmarshalBody},
},
{
Name: "response string",
Resp: &opensearch.Response{
StatusCode: http.StatusForbidden,
Body: io.NopCloser(strings.NewReader(`"Test - Trigger an error"`)),
},
WantedErrors: []error{opensearch.ErrJSONUnmarshalBody},
WantedErrors: []error{opensearch.ErrNonJSONError},
},
{
Name: "error field object",
Expand All @@ -212,13 +198,21 @@ func TestError(t *testing.T) {
WantedErrors: []error{opensearch.ErrJSONUnmarshalBody, opensearch.ErrUnknownOpensearchError},
},
{
Name: "response json",
Name: "unknown json",
Resp: &opensearch.Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(strings.NewReader(`{"_index":"index","_id":"2","matched":false}`)),
},
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 All @@ -232,6 +226,14 @@ func TestError(t *testing.T) {
Resp: &opensearch.Response{StatusCode: http.StatusBadRequest},
WantedErrors: []error{opensearch.ErrUnexpectedEmptyBody},
},
{
Name: "unmarshal error",
Resp: &opensearch.Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(strings.NewReader(`"test"`)),
},
WantedErrors: []error{opensearch.ErrJSONUnmarshalBody},
},
}

for _, tt := range cases {
Expand Down

0 comments on commit dd0c6d8

Please sign in to comment.