You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by jg...@apache.org on 2022/03/01 11:49:49 UTC

[nifi] branch main updated: NIFI-9734 Standardized exception cause message formatting

This is an automated email from the ASF dual-hosted git repository.

jgresock pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new 9bb3418  NIFI-9734 Standardized exception cause message formatting
9bb3418 is described below

commit 9bb34188c98b9ffef24310f0c62a669b9241b3f9
Author: exceptionfactory <ex...@apache.org>
AuthorDate: Sat Feb 26 20:21:37 2022 -0600

    NIFI-9734 Standardized exception cause message formatting
    
    Signed-off-by: Joe Gresock <jg...@gmail.com>
    
    This closes #5813.
---
 .../org/apache/nifi/processor/SimpleProcessLogger.java   | 16 +++++++---------
 .../apache/nifi/processor/TestSimpleProcessLogger.java   | 15 ++++++++++-----
 2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/processor/SimpleProcessLogger.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/processor/SimpleProcessLogger.java
index f3151a0..49b4dd5 100644
--- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/processor/SimpleProcessLogger.java
+++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/processor/SimpleProcessLogger.java
@@ -25,13 +25,12 @@ import org.apache.nifi.logging.LogRepositoryFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.LinkedList;
-import java.util.stream.Collectors;
+import java.util.ArrayList;
+import java.util.List;
 
 public class SimpleProcessLogger implements ComponentLog {
 
-    public static final String NEW_LINE_ARROW = "\u21B3";
-    public static final String CAUSES = NEW_LINE_ARROW + " causes: ";
+    private static final String CAUSED_BY = String.format("%n- Caused by: ");
 
     private final Logger logger;
     private final LogRepository logRepository;
@@ -522,11 +521,10 @@ public class SimpleProcessLogger implements ComponentLog {
     }
 
     private String getCauses(final Throwable throwable) {
-        final LinkedList<String> causes = new LinkedList<>();
-        for (Throwable t = throwable; t != null; t = t.getCause()) {
-            causes.push(t.toString());
+        final List<String> causes = new ArrayList<>();
+        for (Throwable cause = throwable; cause != null; cause = cause.getCause()) {
+            causes.add(cause.toString());
         }
-        return causes.stream().collect(Collectors.joining(System.lineSeparator() + CAUSES));
+        return String.join(CAUSED_BY, causes);
     }
-
 }
diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/processor/TestSimpleProcessLogger.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/processor/TestSimpleProcessLogger.java
index 6d2bd1c5..97a0ba1 100644
--- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/processor/TestSimpleProcessLogger.java
+++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/test/java/org/apache/nifi/processor/TestSimpleProcessLogger.java
@@ -24,7 +24,6 @@ import org.slf4j.Logger;
 
 import java.lang.reflect.Field;
 
-import static org.apache.nifi.processor.SimpleProcessLogger.NEW_LINE_ARROW;
 import static org.junit.Assert.fail;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.eq;
@@ -35,11 +34,17 @@ import static org.mockito.Mockito.when;
 
 public class TestSimpleProcessLogger {
 
-    private static final String EXPECTED_CAUSES = "java.lang.RuntimeException: third" + System.lineSeparator() +
-            NEW_LINE_ARROW + " causes: java.lang.RuntimeException: second" + System.lineSeparator() +
-            NEW_LINE_ARROW + " causes: java.lang.RuntimeException: first";
+    private static final String FIRST_MESSAGE = "FIRST";
+    private static final String SECOND_MESSAGE = "SECOND";
+    private static final String THIRD_MESSAGE = "THIRD";
 
-    private final Exception e = new RuntimeException("first", new RuntimeException("second", new RuntimeException("third")));
+    private static final String EXPECTED_CAUSES = String.join(System.lineSeparator(),
+            String.format("%s: %s", IllegalArgumentException.class.getName(), FIRST_MESSAGE),
+            String.format("- Caused by: %s: %s", RuntimeException.class.getName(), SECOND_MESSAGE),
+            String.format("- Caused by: %s: %s", SecurityException.class.getName(), THIRD_MESSAGE)
+    );
+
+    private final Exception e = new IllegalArgumentException(FIRST_MESSAGE, new RuntimeException(SECOND_MESSAGE, new SecurityException(THIRD_MESSAGE)));
 
     private ReportingTask task;