Skip to content

Commit

Permalink
Log stack trace for Exceptions (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvijayaraman0822 authored Oct 27, 2023
1 parent 807eff8 commit 6cc1c44
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -123,4 +125,24 @@ public static boolean isBlank(final CharSequence cs) {
}
return true;
}


/**
* <p>Gets the stack trace from a Throwable as a String.</p>
*
* <p>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.</p>
*
* @param throwable the <code>Throwable</code> to be examined
* @return the stack trace as generated by the exception's
* <code>printStackTrace(PrintWriter)</code> method
*/
public static String getStackTrace(Throwable throwable) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
throwable.printStackTrace(pw);
return sw.getBuffer().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public static <R> R execute(Callable<R> task, long timeout, TimeUnit unit) {
try {
return future.get(timeout, unit);
} catch (TimeoutException ex) {
LoggerUtil.error("executor-util", "TimeoutException", ex.getMessage());
LoggerUtil.error("executor-util", "TimeoutException", CommonUtils.getStackTrace(ex));
} catch (InterruptedException ex) {
LoggerUtil.error("executor-util", "InterruptedException", ex.getMessage());
LoggerUtil.error("executor-util", "InterruptedException", CommonUtils.getStackTrace(ex));
} catch (ExecutionException ex) {
LoggerUtil.error("executor-util", "ExecutionException", ex.getMessage());
LoggerUtil.error("executor-util", "ExecutionException", CommonUtils.getStackTrace(ex));
} finally {
if (!(future.isDone() || future.isCancelled())) {
future.cancel(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ void testMask_fail_safe() {
EJMask.addFilter(50, "(auth)=([^\\\"]{1,10})[^&]*", "$1xxxx");
String content = "{\"firstName\":\"sensitive data\",\"lastName\":\"sensitive data\",\"nonSensitiveData\":\"firstName\"}";
String result = EJMask.mask(content);
Assertions.assertEquals("masking sensitive content failed due to mock exception please ignore", result);
Assertions.assertTrue(result.contains("masking sensitive content failed due to java.lang.RuntimeException: mock exception please ignore\n" +
"\tat com.ebay.ejmask.api.IContentProcessor.preProcess(IContentProcessor.java:44)\n" +
"\tat com.ebay.ejmask.core.EJMask.process(EJMask.java:174)\n" +
"\tat com.ebay.ejmask.core.EJMask.mask(EJMask.java:153)\n" +
"\tat com.ebay.ejmask.core.EJMask.mask(EJMask.java:100)"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.ebay.ejmask.core.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

class CommonUtilsTest {

@Test
void isEmpty_whenCollectionNullOrEmpty_returnTrue() {
boolean result = CommonUtils.isEmpty(null);
Assertions.assertTrue(result);

boolean result2 = CommonUtils.isEmpty(new ArrayList<>());
Assertions.assertTrue(result2);

}

@Test
void isNotEmpty_whenCollectionIsNotEmpty_returnTrue() {
List<String> list = new ArrayList<>();
list.add("content");

boolean result = CommonUtils.isNotEmpty(list);
Assertions.assertTrue(result);

}

@Test
void emptyIfNull_ifNullCollection_returnEmptyCollection() {
Collection<String> collection = CommonUtils.emptyIfNull(null);
Assertions.assertNotNull(collection);
Assertions.assertEquals(0, collection.size());

}

@Test
void isNotAnEmptyArray_ifEmptyArray_returnsFalse() {
boolean result = CommonUtils.isNotAnEmptyArray(new String[0]);
Assertions.assertFalse(result);

}

@Test
void isAnEmptyArray_ifEmptyArray_returnsFalse() {
boolean result = CommonUtils.isAnEmptyArray(new String[0]);
Assertions.assertTrue(result);

}

@Test
void isNotBlank_ifBlankString_returnsFalse() {
boolean result = CommonUtils.isNotBlank(" ");
Assertions.assertFalse(result);

}

@Test
void isBlank_ifBlankString_returnsTrue() {
boolean result = CommonUtils.isBlank(" ");
Assertions.assertTrue(result);
}

@Test
void getStackTrace_whenExceptionThrown_shouldPrintStackTrace() {
Exception e = new Exception("some exception");
String result = CommonUtils.getStackTrace(e);
Assertions.assertNotNull(result);
Assertions.assertEquals("java.lang.Exception: some exception", result.split("\n")[0]);
}
}

0 comments on commit 6cc1c44

Please sign in to comment.