Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log stack trace for Exceptions #12

Merged
merged 4 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
}
}