diff --git a/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/CobiGenCLI.java b/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/CobiGenCLI.java index e89b8546c8..ee9e94870f 100644 --- a/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/CobiGenCLI.java +++ b/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/CobiGenCLI.java @@ -4,6 +4,7 @@ import java.nio.file.Paths; import java.util.Arrays; +import com.devonfw.cobigen.cli.constants.MessagesConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,11 +35,21 @@ public static void main(String... args) { LOG.debug("Current working directory: {}", System.getProperty("user.dir")); CommandLine commandLine = new CommandLine(new CobiGenCommand()); commandLine.registerConverter(Path.class, s -> Paths.get(s)); + CommandLine.IExecutionExceptionHandler exceptionHandler = new CommandLine.IExecutionExceptionHandler() { + @Override + public int handleExecutionException(Exception e, CommandLine commandLine, CommandLine.ParseResult parseResult) throws Exception { + if (LOG.isDebugEnabled()) { + LOG.debug("Cobigen exited with exception: ", e); + } else { + LOG.error("An error occurred while executing CobiGen: {}", e.getMessage()); + } + return 1; + } + }; + commandLine.setExecutionExceptionHandler(exceptionHandler); int exitCode = commandLine.execute(args); - if (exitCode == 0) { - LOG.info("Success"); - } else { - LOG.info("Failed"); + if (exitCode != 0) { + LOG.error("Cobigen terminated in an unexpected manner. {}", (LOG.isDebugEnabled() ? "" : MessagesConstants.VERBOSE_HINT)); } System.exit(exitCode); } diff --git a/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/CommandCommons.java b/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/CommandCommons.java index ad9ddba393..b3675d77fe 100644 --- a/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/CommandCommons.java +++ b/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/CommandCommons.java @@ -31,6 +31,8 @@ public Integer call() throws Exception { if (this.verbose) { CLILogger.setLevel(Level.DEBUG); + } else { + CLILogger.setLevel(Level.INFO); } return doAction(); diff --git a/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/GenerateCommand.java b/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/GenerateCommand.java index 33f0570b0d..4ea6488af4 100644 --- a/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/GenerateCommand.java +++ b/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/GenerateCommand.java @@ -104,7 +104,6 @@ public Integer doAction() throws Exception { CobiGen cg = CobiGenUtils.initializeCobiGen(this.templatesProject); resolveTemplateDependencies(); - if (this.increments == null && this.templates != null) { Tuple, List> inputsAndArtifacts = preprocess(cg, TemplateTo.class); for (int i = 0; i < inputsAndArtifacts.getA().size(); i++) { @@ -331,16 +330,20 @@ public void generate(Path inputFile, Object input, inputFile); report = cg.generate(input, generableArtifacts, this.outputRootPath.toAbsolutePath(), false, (task, progress) -> { }); - ValidationUtils.checkGenerationReport(report); - Set generatedJavaFiles = report.getGeneratedFiles().stream().filter(e -> e.getFileName().endsWith(".java")) - .collect(Collectors.toSet()); - if (!generatedJavaFiles.isEmpty()) { - try { - ParsingUtils.formatJavaSources(generatedJavaFiles); - } catch (FormatterException e) { - LOG.warn( - "Generation was successful but we were not able to format your code. Maybe you will see strange formatting."); + boolean generationSuccess = ValidationUtils.checkGenerationReport(report); + if (generationSuccess) { + Set generatedJavaFiles = report.getGeneratedFiles().stream().filter(e -> e.getFileName().endsWith(".java")) + .collect(Collectors.toSet()); + if (!generatedJavaFiles.isEmpty()) { + try { + ParsingUtils.formatJavaSources(generatedJavaFiles); + } catch (FormatterException e) { + LOG.warn( + "Generation was successful but we were not able to format your code. Maybe you will see strange formatting."); + } } + } else { + LOG.error("Generation not successful! {}", (LOG.isDebugEnabled() ? "" : MessagesConstants.VERBOSE_HINT)); } } diff --git a/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/constants/MessagesConstants.java b/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/constants/MessagesConstants.java index 9c63da3f73..b288f8e9d4 100644 --- a/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/constants/MessagesConstants.java +++ b/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/constants/MessagesConstants.java @@ -76,4 +76,9 @@ public class MessagesConstants { * Message constant: description of the custom-location option */ public static final String CUSTOM_LOCATION_OPTION_DESCRIPTION = "Custom location where the unpacked templates will be stored."; + + /** + * Message constant: hint for verbose mode. + */ + public static final String VERBOSE_HINT = "For more info execute in verbose mode (-v)."; } diff --git a/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/utils/ValidationUtils.java b/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/utils/ValidationUtils.java index 3c90e493fd..e8dbbb0465 100644 --- a/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/utils/ValidationUtils.java +++ b/cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/utils/ValidationUtils.java @@ -76,28 +76,30 @@ public static boolean isOutputRootPathValid(Path outputRootPath) { * Checks the generation report in order to find possible errors and warnings * * @param report the generation report returned by the CobiGen.generate method + * @return true if generation succeeded, false otherwise */ - public static void checkGenerationReport(GenerationReportTo report) { + public static boolean checkGenerationReport(GenerationReportTo report) { for (String warning : report.getWarnings()) { LOG.debug("Warning: {}", warning); } + // Successful generation if (report.getErrors() == null || report.getErrors().isEmpty()) { LOG.info("Successful generation."); - } else { - if (LOG.isDebugEnabled() && report.getErrors().size() > 1) { - for (int i = 1; i < report.getErrors().size(); i++) { - LOG.error("Further reported error:", report.getErrors().get(i)); - } - } - if (report.getErrors().get(0) instanceof CobiGenRuntimeException) { - throw report.getErrors().get(0); + return true; + } + // Unsuccesful generation, log error messages to user + // Log exceptions for devs in DEBUG + for (int i = 0; i < report.getErrors().size(); i++) { + RuntimeException e = report.getErrors().get(i); + if (LOG.isDebugEnabled()) { + LOG.debug("Encountered an exception: ", e); } else { - throw new CobiGenRuntimeException("Generation failed. Enable debug mode to see the exceptions occurred.", - report.getErrors().get(0)); + LOG.error("Error: {}", e.getMessage()); } } + return false; } /** diff --git a/cobigen-plugins/cobigen-templateengines/cobigen-tempeng-freemarker/src/main/java/com/devonfw/cobigen/tempeng/freemarker/FreeMarkerTemplateEngine.java b/cobigen-plugins/cobigen-templateengines/cobigen-tempeng-freemarker/src/main/java/com/devonfw/cobigen/tempeng/freemarker/FreeMarkerTemplateEngine.java index 4a27d04fec..e112b61d0b 100644 --- a/cobigen-plugins/cobigen-templateengines/cobigen-tempeng-freemarker/src/main/java/com/devonfw/cobigen/tempeng/freemarker/FreeMarkerTemplateEngine.java +++ b/cobigen-plugins/cobigen-templateengines/cobigen-tempeng-freemarker/src/main/java/com/devonfw/cobigen/tempeng/freemarker/FreeMarkerTemplateEngine.java @@ -73,8 +73,21 @@ public void process(TextTemplate template, Map model, Writer out env.setLogTemplateExceptions(false); // no duplicate logging env.process(); } catch (TemplateException e) { + // Get root cause + Throwable rootCause = e; + while (rootCause.getCause() != null && rootCause.getCause() != rootCause) { + rootCause = rootCause.getCause(); + } + String detailedCause = ""; + + if (rootCause.getClass().getCanonicalName().contains("java.lang")) { + detailedCause = ". A problem with Reflection has likely occurred: " + "Exception of type: " + + rootCause.getClass().getCanonicalName() + " with message: " + rootCause.getMessage() + + ", please consider rebuilding your project as a possible fix."; + } throw new CobiGenRuntimeException("An error occurred while generating the template: " - + template.getAbsoluteTemplatePath() + " (FreeMarker v" + FreemarkerMetadata.VERSION + ")", e); + + template.getAbsoluteTemplatePath() + " (FreeMarker v" + FreemarkerMetadata.VERSION + ")" + detailedCause, + e); } catch (Throwable e) { throw new CobiGenRuntimeException("An unkonwn error occurred while generating the template: " + template.getAbsoluteTemplatePath() + " (FreeMarker v" + FreemarkerMetadata.VERSION + ")", e); diff --git a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/generator/InputInterpreterImpl.java b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/generator/InputInterpreterImpl.java index ff42503b2d..de935e7fbf 100644 --- a/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/generator/InputInterpreterImpl.java +++ b/cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/generator/InputInterpreterImpl.java @@ -104,8 +104,15 @@ private Object readInput(Path path, Charset inputCharset, Map