From 6cc1c448c7623b750b82f0a6141334e372adc565 Mon Sep 17 00:00:00 2001 From: Vasudev Vijayaraman <35582753+vvijayaraman0822@users.noreply.github.com> Date: Fri, 27 Oct 2023 14:01:25 -0400 Subject: [PATCH] Log stack trace for Exceptions (#12) --- README.md | 4 +- .../java/com/ebay/ejmask/core/EJMask.java | 2 +- .../ebay/ejmask/core/util/CommonUtils.java | 22 ++++++ .../ebay/ejmask/core/util/ExecutorUtil.java | 6 +- .../java/com/ebay/ejmask/core/EJMaskTest.java | 6 +- .../ejmask/core/util/CommonUtilsTest.java | 74 +++++++++++++++++++ 6 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 ejmask-core/src/test/java/com/ebay/ejmask/core/util/CommonUtilsTest.java diff --git a/README.md b/README.md index 0ebf97c..5e7d6f5 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ public class JsonPatternBuilder implements IPatternBuilder { } ``` #### Extensions -For many standard use cases you can make use of patten builders defined in `ejmask-extensions` module. +For many standard use cases you can make use of pattern builders defined in `ejmask-extensions` module. - HeaderFieldPatternBuilder - XmlFieldPattenBuilder @@ -91,7 +91,7 @@ public class Sample implements IFilter { ### ContentProcessor `ContentProcessor`(s) configured with the data masker will be invoked to process the data before and after actual masking operations getting invoked. -A few usecase we can use is to decode and encode the sting before masking or to reduce the size of a large string before performing the masking operation to improve performance. +A few use case we can use is to decode and encode the string before masking and/or to reduce the size of a large string before performing the masking operation to improve performance. #### Extensions - ContentSlicerPreProcessor diff --git a/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java b/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java index e0f17b3..2cd0100 100644 --- a/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java +++ b/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java @@ -158,7 +158,7 @@ public static String mask(final String content, boolean preProcessingRequired, b } return contentInProgress; } catch (Exception ex) { - return "masking sensitive content failed due to " + ex.getMessage(); + return "masking sensitive content failed due to " + CommonUtils.getStackTrace(ex); } } diff --git a/ejmask-core/src/main/java/com/ebay/ejmask/core/util/CommonUtils.java b/ejmask-core/src/main/java/com/ebay/ejmask/core/util/CommonUtils.java index 836b5a2..022b8ef 100644 --- a/ejmask-core/src/main/java/com/ebay/ejmask/core/util/CommonUtils.java +++ b/ejmask-core/src/main/java/com/ebay/ejmask/core/util/CommonUtils.java @@ -17,6 +17,8 @@ */ +import java.io.PrintWriter; +import java.io.StringWriter; import java.lang.reflect.Array; import java.util.Collection; import java.util.Collections; @@ -123,4 +125,24 @@ public static boolean isBlank(final CharSequence cs) { } return true; } + + + /** + *
Gets the stack trace from a Throwable as a String.
+ * + *The result of this method vary by JDK version as this method + * uses {@link Throwable#printStackTrace(java.io.PrintWriter)}. + * On JDK1.3 and earlier, the cause exception will not be shown + * unless the specified throwable alters printStackTrace.
+ * + * @param throwable theThrowable
to be examined
+ * @return the stack trace as generated by the exception's
+ * printStackTrace(PrintWriter)
method
+ */
+ public static String getStackTrace(Throwable throwable) {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw, true);
+ throwable.printStackTrace(pw);
+ return sw.getBuffer().toString();
+ }
}
diff --git a/ejmask-core/src/main/java/com/ebay/ejmask/core/util/ExecutorUtil.java b/ejmask-core/src/main/java/com/ebay/ejmask/core/util/ExecutorUtil.java
index 7c4a27a..4684cc9 100644
--- a/ejmask-core/src/main/java/com/ebay/ejmask/core/util/ExecutorUtil.java
+++ b/ejmask-core/src/main/java/com/ebay/ejmask/core/util/ExecutorUtil.java
@@ -56,11 +56,11 @@ public static