From c685fa654da4cfe6a12a5a6c7aaaadc4c4dc1247 Mon Sep 17 00:00:00 2001 From: Martin Gressler Date: Fri, 12 Apr 2024 02:39:28 -0700 Subject: [PATCH] Fixes opensearch-project/opensearch-go#522 (#523) * Fixes opensearch-project/opensearch-go#522 Signed-off-by: Martin Gressler * handle non-json error by returning body Signed-off-by: Martin Gressler * add to changelog Signed-off-by: Martin Gressler --------- Signed-off-by: Martin Gressler --- CHANGELOG.md | 1 + error.go | 8 +------- error_test.go | 35 +++++++++++++++++++---------------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5d3bd508..da9b3ad0c 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 5048f4bbe..89795f2ef 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 { diff --git a/error_test.go b/error_test.go index cd9b46647..56dac18ae 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") + }) }) }