From 6252b6f6f84e1893e82a1db8138d4fa8c9be7a62 Mon Sep 17 00:00:00 2001 From: arnitolog <34036079+arnitolog@users.noreply.github.com> Date: Wed, 13 Nov 2024 22:00:00 -0600 Subject: [PATCH 1/4] properly handle biri.Error --- internal/impl/opensearch/output.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/internal/impl/opensearch/output.go b/internal/impl/opensearch/output.go index 21dccc09f..14ce07b99 100644 --- a/internal/impl/opensearch/output.go +++ b/internal/impl/opensearch/output.go @@ -382,13 +382,24 @@ func (e *Output) buildBulkableRequest(p *pendingBulkIndex, onError func(err erro biri opensearchapi.BulkRespItem, err error, ) { - if err == nil { - if biri.Error.Type == "" { - biri.Error.Type = fmt.Sprintf("status %v", biri.Status) + if err != nil { + onError(err) + return + } + + errType := fmt.Sprintf("status %v", biri.Status) + errReason := "unknown error" + + if biri.Error != nil { + if biri.Error.Type != "" { + errType = biri.Error.Type + } + if biri.Error.Reason != "" { + errReason = biri.Error.Reason } - err = fmt.Errorf("%v: %v", biri.Error.Type, biri.Error.Reason) } - onError(err) + + onError(fmt.Errorf("%v: %v", errType, errReason)) } return } From 3caa316dc6d733c6ee0ab36e8cf8a79d623df6af Mon Sep 17 00:00:00 2001 From: arnitolog <34036079+arnitolog@users.noreply.github.com> Date: Thu, 14 Nov 2024 13:02:44 -0600 Subject: [PATCH 2/4] added integration tests for deleting non-existing document case --- internal/impl/opensearch/integration_test.go | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/internal/impl/opensearch/integration_test.go b/internal/impl/opensearch/integration_test.go index 46de802e8..0e5fbcfa6 100644 --- a/internal/impl/opensearch/integration_test.go +++ b/internal/impl/opensearch/integration_test.go @@ -451,6 +451,41 @@ action: ${! @elastic_action } resEqualsJSON(t, get, string(testMsg[i])) } } + + // Test deleting a non-existing document + m2 := outputFromConf(t, ` +index: test_conn_index +id: 'non-existing-id' +urls: %v +action: delete +`, urls) + + require.NoError(t, m2.Connect(ctx)) + defer func() { + require.NoError(t, m2.Close(ctx)) + }() + + require.Error(t, m2.WriteBatch(ctx, service.MessageBatch{ + service.NewMessage([]byte{}), + })) + + // Verify the document was not found + get, err := client.Do(ctx, osapi.DocumentGetReq{ + Index: "test_conn_index", + DocumentID: "non-existing-id", + }, nil) + require.NoError(t, err) + if get.IsError() { + var respCode int + respCode = get.StatusCode + if respCode == http.StatusNotFound { + // Document was not found, as expected + } else { + t.Errorf("Unexpected error deleting non-existing document: %d", respCode) + } + } else { + t.Errorf("Expected error deleting non-existing document") + } } func testOpenSearchBatchIDCollision(urls []string, client *os.Client, t *testing.T) { From 09f8b76cb670731f2a62ae74cadaa26500f813ab Mon Sep 17 00:00:00 2001 From: arnitolog <34036079+arnitolog@users.noreply.github.com> Date: Thu, 14 Nov 2024 14:41:38 -0600 Subject: [PATCH 3/4] fixed lint --- internal/impl/opensearch/integration_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/impl/opensearch/integration_test.go b/internal/impl/opensearch/integration_test.go index 0e5fbcfa6..f3c5057bd 100644 --- a/internal/impl/opensearch/integration_test.go +++ b/internal/impl/opensearch/integration_test.go @@ -476,8 +476,7 @@ action: delete }, nil) require.NoError(t, err) if get.IsError() { - var respCode int - respCode = get.StatusCode + respCode := get.StatusCode if respCode == http.StatusNotFound { // Document was not found, as expected } else { From d9c4b4bcbad28f7e9afa492a1c32c970c0c00328 Mon Sep 17 00:00:00 2001 From: Greg Furman Date: Fri, 15 Nov 2024 10:33:19 +0200 Subject: [PATCH 4/4] Fix golangci-lint having a diva moment --- internal/impl/opensearch/integration_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/impl/opensearch/integration_test.go b/internal/impl/opensearch/integration_test.go index f3c5057bd..7df03da0d 100644 --- a/internal/impl/opensearch/integration_test.go +++ b/internal/impl/opensearch/integration_test.go @@ -451,7 +451,7 @@ action: ${! @elastic_action } resEqualsJSON(t, get, string(testMsg[i])) } } - + // Test deleting a non-existing document m2 := outputFromConf(t, ` index: test_conn_index @@ -476,8 +476,7 @@ action: delete }, nil) require.NoError(t, err) if get.IsError() { - respCode := get.StatusCode - if respCode == http.StatusNotFound { + if respCode := get.StatusCode; respCode == http.StatusNotFound { // Document was not found, as expected } else { t.Errorf("Unexpected error deleting non-existing document: %d", respCode)