From 451a88df438f1ea2ed14c862a95aaed13b8c1ee3 Mon Sep 17 00:00:00 2001 From: Andre F de Miranda Date: Sat, 1 Apr 2017 19:12:58 +1100 Subject: [PATCH] NIFI-11 - Capture Exception to prevent failed evaluations from yielding processor - Further capture evaluation exceptions as per PR feedback - Adjust jUnit to new exception behavior - This closes #1644 --- .../attributes/UpdateAttribute.java | 17 +++++++++----- .../attributes/TestUpdateAttribute.java | 22 ++++++++++++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/nifi-nar-bundles/nifi-update-attribute-bundle/nifi-update-attribute-processor/src/main/java/org/apache/nifi/processors/attributes/UpdateAttribute.java b/nifi-nar-bundles/nifi-update-attribute-bundle/nifi-update-attribute-processor/src/main/java/org/apache/nifi/processors/attributes/UpdateAttribute.java index a64279d6c6..1ea0b61f90 100644 --- a/nifi-nar-bundles/nifi-update-attribute-bundle/nifi-update-attribute-processor/src/main/java/org/apache/nifi/processors/attributes/UpdateAttribute.java +++ b/nifi-nar-bundles/nifi-update-attribute-bundle/nifi-update-attribute-processor/src/main/java/org/apache/nifi/processors/attributes/UpdateAttribute.java @@ -579,8 +579,9 @@ public class UpdateAttribute extends AbstractProcessor implements Searchable { try { // evaluate the expression for the given flow file return getPropertyValue(condition.getExpression(), context).evaluateAttributeExpressions(flowfile, null, null, statefulAttributes).asBoolean(); - } catch (final ProcessException pe) { - throw new ProcessException(String.format("Unable to evaluate condition '%s': %s.", condition.getExpression(), pe), pe); + } catch (final Exception e) { + getLogger().error(String.format("Could not evaluate the condition '%s' while processing Flowfile '%s'", condition.getExpression(), flowfile)); + throw new ProcessException(String.format("Unable to evaluate condition '%s': %s.", condition.getExpression(), e), e); } } @@ -643,8 +644,9 @@ public class UpdateAttribute extends AbstractProcessor implements Searchable { // No point in updating if they will be removed attributesToUpdate.keySet().removeAll(attributesToDelete); } - } catch (final ProcessException pe) { - throw new ProcessException(String.format("Unable to delete attribute '%s': %s.", attribute, pe), pe); + } catch (final Exception e) { + logger.error(String.format("Unable to delete attribute '%s' while processing FlowFile '%s' .", attribute, flowfile)); + throw new ProcessException(String.format("Unable to delete attribute '%s': %s.", attribute, e), e); } } else { boolean notDeleted = !attributesToDelete.contains(attribute); @@ -667,8 +669,11 @@ public class UpdateAttribute extends AbstractProcessor implements Searchable { if (notDeleted) { attributesToUpdate.put(attribute, newAttributeValue); } - } catch (final ProcessException pe) { - throw new ProcessException(String.format("Unable to evaluate new value for attribute '%s': %s.", attribute, pe), pe); + // Capture Exception thrown when evaluating the Expression Language + } catch (final Exception e) { + logger.error(String.format("Could not evaluate the FlowFile '%s' against expression '%s' " + + "defined by DynamicProperty '%s' due to '%s'", flowfile, action.getValue(), attribute, e.getLocalizedMessage())); + throw new ProcessException(String.format("Unable to evaluate new value for attribute '%s': %s.", attribute, e), e); } } } diff --git a/nifi-nar-bundles/nifi-update-attribute-bundle/nifi-update-attribute-processor/src/test/java/org/apache/nifi/update/attributes/TestUpdateAttribute.java b/nifi-nar-bundles/nifi-update-attribute-bundle/nifi-update-attribute-processor/src/test/java/org/apache/nifi/update/attributes/TestUpdateAttribute.java index 5c00a20b7f..8d3499cdbd 100644 --- a/nifi-nar-bundles/nifi-update-attribute-bundle/nifi-update-attribute-processor/src/test/java/org/apache/nifi/update/attributes/TestUpdateAttribute.java +++ b/nifi-nar-bundles/nifi-update-attribute-bundle/nifi-update-attribute-processor/src/test/java/org/apache/nifi/update/attributes/TestUpdateAttribute.java @@ -29,6 +29,7 @@ import java.util.regex.PatternSyntaxException; import org.apache.nifi.components.state.Scope; import org.apache.nifi.processor.ProcessSessionFactory; +import org.apache.nifi.processor.exception.ProcessException; import org.apache.nifi.processors.attributes.UpdateAttribute; import org.apache.nifi.state.MockStateManager; import org.apache.nifi.update.attributes.serde.CriteriaSerDe; @@ -36,6 +37,7 @@ import org.apache.nifi.util.MockFlowFile; import org.apache.nifi.util.TestRunner; import org.apache.nifi.util.TestRunners; +import org.junit.Assert; import org.junit.Test; import static org.apache.nifi.processors.attributes.UpdateAttribute.STORE_STATE_LOCALLY; @@ -983,7 +985,25 @@ public class TestUpdateAttribute { try { runner.run(); } catch (Throwable t) { - assertEquals(t.getCause().getClass(), PatternSyntaxException.class); + assertEquals(ProcessException.class, t.getCause().getClass()); } } + + @Test + public void testDataIsTooShort() { + final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute()); + runner.setProperty("attribute.1", "${test:substring(1, 20)}"); + + runner.assertValid(); + + final Map attributes = new HashMap<>(); + attributes.put("test", "chocolate"); + runner.enqueue(new byte[0], attributes); + try { + runner.run(); + } catch (AssertionError e) { + Assert.assertTrue(e.getMessage().contains("org.apache.nifi.processor.exception.ProcessException")); + } + } + }