Skip to content

Commit

Permalink
[MENFORCER-415] Move error message to MojoException
Browse files Browse the repository at this point in the history
In multimodal project it is very difficult to find a reason of failed job.
  • Loading branch information
slawekjaranowski committed Jan 25, 2023
1 parent 26697b1 commit 4bbf7cd
Showing 1 changed file with 11 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
*/
package org.apache.maven.plugins.enforcer;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -228,9 +227,6 @@ public void execute() throws MojoExecutionException {
}
}

// messages with warn/error flag
Map<String, Boolean> messages = new LinkedHashMap<>();

// create my helper
PluginParameterExpressionEvaluator evaluator = new PluginParameterExpressionEvaluator(session, mojoExecution);
EnforcerRuleHelper helper = new DefaultEnforcementRuleHelper(session, evaluator, log, container);
Expand All @@ -241,7 +237,7 @@ public void execute() throws MojoExecutionException {
failFast = false;
}

boolean hasErrors = false;
List<String> errorMessages = new ArrayList<>();

// go through each rule
for (int ruleIndex = 0; ruleIndex < rulesList.size(); ruleIndex++) {
Expand All @@ -252,36 +248,30 @@ public void execute() throws MojoExecutionException {
executeRule(ruleIndex, ruleDesc, helper);
} catch (EnforcerRuleError e) {
String ruleMessage = createRuleMessage(ruleIndex, ruleDesc, EnforcerLevel.ERROR, e);
throw new MojoExecutionException(ruleMessage, e);
throw new MojoExecutionException(System.lineSeparator() + ruleMessage, e);
} catch (EnforcerRuleException e) {

String ruleMessage = createRuleMessage(ruleIndex, ruleDesc, level, e);

if (failFast && level == EnforcerLevel.ERROR) {
throw new MojoExecutionException(ruleMessage, e);
throw new MojoExecutionException(System.lineSeparator() + ruleMessage, e);
}

if (level == EnforcerLevel.ERROR) {
hasErrors = true;
messages.put(ruleMessage, true);
errorMessages.add(ruleMessage);
} else {
messages.put(ruleMessage, false);
log.warn(ruleMessage);
}
}
}

// log any messages
messages.forEach((message, error) -> {
if (fail && error) {
log.error(message);
if (!errorMessages.isEmpty()) {
if (fail) {
throw new MojoExecutionException(
System.lineSeparator() + String.join(System.lineSeparator(), errorMessages));
} else {
log.warn(message);
errorMessages.forEach(log::warn);
}
});

if (fail && hasErrors) {
throw new MojoExecutionException(
"Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed.");
}
}

Expand Down

0 comments on commit 4bbf7cd

Please sign in to comment.