Skip to content

Commit

Permalink
Improves ParseError response when server response is an unknown json (#…
Browse files Browse the repository at this point in the history
…592)

* Improves ParseError response when server response is an unknown json

Signed-off-by: vatsal <[email protected]>

* Improves ParseError response when server response is an unknown json

Signed-off-by: vatsal <[email protected]>

* Improves ParseError response when server response is an unknown json

Signed-off-by: vatsal <[email protected]>

* Improves ParseError response when server response is an unknown json

Signed-off-by: vatsal <[email protected]>

---------

Signed-off-by: vatsal <[email protected]>
  • Loading branch information
imvtsl authored Jul 23, 2024
1 parent bbcb0da commit d3e1f10
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fixes wrong response parsing for security get requests ([#572](https://github.com/opensearch-project/opensearch-go/pull/572))
- Fixes opensearchtransport ignores request context cancellation when `retryBackoff` is configured ([#540](https://github.com/opensearch-project/opensearch-go/pull/540))
- Fixes opensearchtransport sleeps unexpectedly after the last retry ([#540](https://github.com/opensearch-project/opensearch-go/pull/540))
- Improves ParseError response when server response is an unknown json ([#592](https://github.com/opensearch-project/opensearch-go/pull/592))

### Security

Expand Down
40 changes: 40 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,46 @@

# Upgrading Opensearch GO Client

## Upgrading to >= 5.0.0
Version 5.0.0 returns `*opensearch.StringError` error type instead of `*fmt.wrapError` when response received from the server is an unknown JSON. For example, consider delete document API which returns an unknown JSON body when document is not found.

Before 5.0.0:
```go
docDelResp, err = client.Document.Delete(ctx, opensearchapi.DocumentDeleteReq{Index: "movies", DocumentID: "3"})
if err != nil {
fmt.Println(err)

if !errors.Is(err, opensearch.ErrJSONUnmarshalBody) && docDelResp != nil {
resp := docDelResp.Inspect().Response
// get http status
fmt.Println(resp.StatusCode)
body := strings.TrimPrefix(err.Error(), "opensearch error response could not be parsed as error: ")
errResp := opensearchapi.DocumentDeleteResp{}
json.Unmarshal([]byte(body), &errResp)
// extract result field from the body
fmt.Println(errResp.Result)
}
}
```

After 5.0.0:
```go
docDelResp, err = client.Document.Delete(ctx, opensearchapi.DocumentDeleteReq{Index: "movies", DocumentID: "3"})
if err != nil {
// parse into *opensearch.StringError
var myStringErr *opensearch.StringError
if errors.As(err, &myStringErr) {
// get http status
fmt.Println(myStringErr.Status)
errResp := opensearchapi.DocumentDeleteResp{}
json.Unmarshal([]byte(myStringErr.Err), &errResp)
// extract result field from the body
fmt.Println(errResp.Result)
}
}
```


## Upgrading to >= 4.0.0

Version 4.0.0 moved the error types, added with 3.0.0, from opensearchapi to opensearch, renamed them and added new error types.
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func ParseError(resp *Response) error {
return parseError(body, &apiError)
}

return fmt.Errorf("%w: %s", ErrUnknownOpensearchError, string(body))
return &StringError{Status: resp.StatusCode, Err: string(body)}
}

func parseError(body []byte, errStruct error) error {
Expand Down
24 changes: 16 additions & 8 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ func TestError(t *testing.T) {
_ = fmt.Sprintf("%s", testError)
})

t.Run("StringErrorUnknownJSON", func(t *testing.T) {
resp := &opensearch.Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(
strings.NewReader(`{"_index":"index","_id":"2","matched":false}`),
),
}
assert.True(t, resp.IsError())
err := opensearch.ParseError(resp)
var testError *opensearch.StringError
require.True(t, errors.As(err, &testError))
assert.Equal(t, http.StatusNotFound, testError.Status)
assert.Contains(t, testError.Err, "{\"_index\":\"index\",\"_id\":\"2\",\"matched\":false}")
_ = fmt.Sprintf("%s", testError)
})

t.Run("Error", func(t *testing.T) {
resp := &opensearch.Response{
StatusCode: http.StatusBadRequest,
Expand Down Expand Up @@ -189,14 +205,6 @@ func TestError(t *testing.T) {
},
WantedErrors: []error{opensearch.ErrJSONUnmarshalBody, opensearch.ErrUnknownOpensearchError},
},
{
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: "io read error",
Resp: &opensearch.Response{
Expand Down

0 comments on commit d3e1f10

Please sign in to comment.