Skip to content

Commit

Permalink
Merge pull request #19 from No3x/patch-1
Browse files Browse the repository at this point in the history
Improve exception handling with InvocationTargetException
  • Loading branch information
seaside1 authored Jan 10, 2022
2 parents 22a538c + e2fc00e commit 88d4d7d
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -466,8 +469,21 @@ private synchronized void invokeRule(JRuleExecutionContext context, JRuleEvent e
rule.setRuleLogName(context.getLogName());
try {
final Object invoke = context.isEventParameterPresent() ? method.invoke(rule, event) : method.invoke(rule);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
JRuleLog.error(logger, context.getRuleName(), "Error", e);
} catch (IllegalAccessException | IllegalArgumentException | SecurityException e) {
JRuleLog.error(logger, context.getRuleName(), "Error {}", e);
} catch (InvocationTargetException e) {
Throwable ex = e.getCause() != null ? e.getCause() : null;
JRuleLog.error(logger, context.getRuleName(), "Error message: {}", ex.getMessage());
JRuleLog.error(logger, context.getRuleName(), "Error Stacktrace: {}", getStackTraceAsString(ex));
}
}

private synchronized static String getStackTraceAsString(Throwable throwable) {
try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
throwable.printStackTrace(pw);
return sw.toString();
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
}

0 comments on commit 88d4d7d

Please sign in to comment.