MikeThomsen commented on code in PR #6903: URL: https://github.com/apache/nifi/pull/6903#discussion_r1114410908
########## nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/groovy/org/apache/nifi/processors/elasticsearch/AbstractPutElasticsearchTest.groovy: ########## @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License") you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.nifi.processors.elasticsearch + +import org.apache.nifi.util.TestRunner +import org.apache.nifi.util.TestRunners +import org.junit.jupiter.api.Test + +import static org.hamcrest.CoreMatchers.hasItem +import static org.hamcrest.CoreMatchers.not +import static org.hamcrest.MatcherAssert.assertThat + +abstract class AbstractPutElasticsearchTest<P extends AbstractPutElasticsearch> { Review Comment: Please convert this to a Java class as we are starting to move in that direction for testing. ########## nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/main/java/org/apache/nifi/processors/elasticsearch/AbstractPutElasticsearch.java: ########## @@ -158,15 +200,38 @@ void transferFlowFilesOnException(final Exception ex, final Relationship rel, fi } } - void logElasticsearchDocumentErrors(final IndexOperationResponse response) throws JsonProcessingException { - if (logErrors || getLogger().isDebugEnabled()) { - final List<Map<String, Object>> errors = response.getItems(); - final String output = String.format("An error was encountered while processing bulk operations. Server response below:%n%n%s", errorMapper.writeValueAsString(errors)); + void handleElasticsearchDocumentErrors(final IndexOperationResponse response, final List<Integer> errorIndices, final ProcessSession session, final FlowFile parent) throws IOException { + if (!errorIndices.isEmpty() && (outputErrorResponses || logErrors || getLogger().isDebugEnabled())) { + final List<Map<String, Object>> errorResponses = errorIndices.stream().map(index -> response.getItems().get(index)).collect(Collectors.toList()); - if (logErrors) { - getLogger().error(output); - } else { - getLogger().debug(output); + if (logErrors || getLogger().isDebugEnabled()) { + final String output = String.format( + "An error was encountered while processing bulk operations. Server response below:%n%n%s", + errorMapper.writeValueAsString(errorResponses) + ); + + if (logErrors) { + getLogger().error(output); + } else { + getLogger().debug(output); + } + } + + if (outputErrorResponses) { + FlowFile errorResponsesFF = null; + try { + errorResponsesFF = session.create(parent); + try (final OutputStream errorsOutputStream = session.write(errorResponsesFF)) { + errorMapper.writeValue(errorsOutputStream, errorResponses); + + errorResponsesFF = session.putAttribute(errorResponsesFF, "elasticsearch.put.error.count", String.valueOf(errorResponses.size())); Review Comment: Have you tested this live? I've had problems with this in the past where the ProcessSession didn't want to update the attributes while the OutputStream was open. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
