Repository: opensearch-project/opensearch-go
Pull request #523 (2024-04-10 14:00:59)

Fixes opensearch-project/opensearch-go#522
### Description
Add test for 429 example response. Handle the error with an added sentinel.


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/523?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 66.16%. Comparing base [(`06a6dc8`)](https://app.codecov.io/gh/opensearch-project/opensearch-go/commit/06a6dc8cbaeb8d1e3657487661edd4900e4f31df?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) to head [(`5e3cc21`)](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/523?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project).
> Report is 4 commits behind head on main.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #523      +/-   ##
==========================================
+ Coverage   57.29%   66.16%   +8.86%     
==========================================
  Files         315      363      +48     
  Lines        9823     8567    -1256     
==========================================
+ Hits         5628     5668      +40     
+ Misses       2902     1588    -1314     
- Partials     1293     1311      +18     
```

| [Flag](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/523/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) | Coverage Δ | |
|---|---|---|
| [integration](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/523/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) | `59.41% <0.00%> (+8.57%)` | :arrow_up: |
| [unit](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/523/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) | `81.54% <100.00%> (+68.69%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Files](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/523?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project) | Coverage Δ | |
|---|---|---|
| [error.go](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/523?src=pr&el=tree&filepath=error.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project#diff-ZXJyb3IuZ28=) | `100.00% <100.00%> (ø)` | |

... and [250 files with indirect coverage changes](https://app.codecov.io/gh/opensearch-project/opensearch-go/pull/523/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=opensearch-project)

</details>
dblock: Add a line to CHANGELOG please and we're good to go.
dblock: Also see https://github.com/opensearch-project/opensearch-go/issues/522#issuecomment-2047653280
dblock: @martingrzzler You have to sign all your commits with DCO please (`git commit -s --amend` and force push).

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e5d3bd5..da9b3ad 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
 
diff --git a/error.go b/error.go
index 5048f4b..89795f2 100644
--- a/error.go
+++ b/error.go
@@ -11,7 +11,6 @@ import (
 	"errors"
 	"fmt"
 	"io"
-	"net/http"
 )
 
 // Error vars
@@ -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
@@ -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 {


Test patch:
diff --git a/error_test.go b/error_test.go
index cd9b466..56dac18 100644
--- a/error_test.go
+++ b/error_test.go
@@ -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{
@@ -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{
@@ -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")
+		})
 	})
 }