diff --git a/internal/impl/opensearch/integration_test.go b/internal/impl/opensearch/integration_test.go index 46de802e8..7df03da0d 100644 --- a/internal/impl/opensearch/integration_test.go +++ b/internal/impl/opensearch/integration_test.go @@ -451,6 +451,39 @@ 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() { + if respCode := get.StatusCode; 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) { 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 }