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

Improves ParseError response when server response is an unknown json #592

Merged
merged 4 commits into from
Jul 23, 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 @@ -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
Loading