diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSet.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSet.java index 1b0574b8ea..1b7585882e 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSet.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSet.java @@ -504,6 +504,7 @@ public class RuleSet implements ChecksumAware { } } catch (RuntimeException e) { if (ctx.isIgnoreExceptions()) { + ctx.getReport().addError(new Report.ProcessingError(e, ctx.getSourceCodeFilename())); if (LOG.isLoggable(Level.WARNING)) { LOG.log(Level.WARNING, "Exception applying rule " + rule.getName() + " on file " + ctx.getSourceCodeFilename() + ", continuing with next rule", e); diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetTest.java index 7260e089c3..429136f67b 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/RuleSetTest.java @@ -22,6 +22,7 @@ import java.util.Set; import org.junit.Test; +import net.sourceforge.pmd.Report.ProcessingError; import net.sourceforge.pmd.RuleSet.RuleSetBuilder; import net.sourceforge.pmd.lang.Dummy2LanguageModule; import net.sourceforge.pmd.lang.DummyLanguageModule; @@ -30,6 +31,7 @@ import net.sourceforge.pmd.lang.ast.DummyNode; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.rule.MockRule; import net.sourceforge.pmd.lang.rule.RuleReference; +import net.sourceforge.pmd.util.CollectionUtil; public class RuleSetTest { @@ -507,4 +509,46 @@ public class RuleSetTest { nodes.add(node); return nodes; } + + @Test + public void ruleExceptionShouldBeReported() { + RuleSet ruleset = createRuleSetBuilder("ruleExceptionShouldBeReported") + .addRule(new MockRule() { + @Override + public void apply(List nodes, RuleContext ctx) { + throw new RuntimeException("Test exception while applying rule"); + } + }) + .build(); + RuleContext context = new RuleContext(); + context.setReport(new Report()); + context.setLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getDefaultVersion()); + context.setSourceCodeFilename(RuleSetTest.class.getName() + ".ruleExceptionShouldBeReported"); + context.setIgnoreExceptions(true); // the default + ruleset.apply(makeCompilationUnits(), context); + + assertTrue("Report should have processing errors", context.getReport().hasErrors()); + List errors = CollectionUtil.toList(context.getReport().errors()); + assertEquals("Errors expected", 1, errors.size()); + assertEquals("Wrong error message", "Test exception while applying rule", errors.get(0).getMsg()); + assertTrue("Should be a RuntimeException", errors.get(0).getError() instanceof RuntimeException); + } + + @Test(expected = RuntimeException.class) + public void ruleExceptionShouldBeThrownIfNotIgnored() { + RuleSet ruleset = createRuleSetBuilder("ruleExceptionShouldBeReported") + .addRule(new MockRule() { + @Override + public void apply(List nodes, RuleContext ctx) { + throw new RuntimeException("Test exception while applying rule"); + } + }) + .build(); + RuleContext context = new RuleContext(); + context.setReport(new Report()); + context.setLanguageVersion(LanguageRegistry.getLanguage(DummyLanguageModule.NAME).getDefaultVersion()); + context.setSourceCodeFilename(RuleSetTest.class.getName() + ".ruleExceptionShouldBeThrownIfNotIgnored"); + context.setIgnoreExceptions(false); + ruleset.apply(makeCompilationUnits(), context); + } }