From f2c4b2c95addf45e0ada75d7d9358881b3c5dcc0 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 5 Feb 2025 10:52:56 +0100 Subject: [PATCH] Report first `Throwable` caught in `InMemoryExecutionContext.onError` (#942) * Report first Throwable caught in InMemoryExecutionContext.onError * Log a warning for any caught throwable instead * Switch to a list of warnings to have the full details * Update src/main/java/org/openrewrite/maven/AbstractRewriteDryRunMojo.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Remove unused imports * Remove unused imports * Restore the debug log, and provide hint when not enabled --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../maven/AbstractRewriteDryRunMojo.java | 12 +++++++++++- .../openrewrite/maven/AbstractRewriteMojo.java | 7 +++++-- .../openrewrite/maven/AbstractRewriteRunMojo.java | 15 ++++++++++++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/openrewrite/maven/AbstractRewriteDryRunMojo.java b/src/main/java/org/openrewrite/maven/AbstractRewriteDryRunMojo.java index 3b52c221..4fc97728 100644 --- a/src/main/java/org/openrewrite/maven/AbstractRewriteDryRunMojo.java +++ b/src/main/java/org/openrewrite/maven/AbstractRewriteDryRunMojo.java @@ -28,6 +28,8 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.time.Duration; +import java.util.ArrayList; +import java.util.List; import java.util.stream.Stream; /** @@ -66,7 +68,9 @@ public void execute() throws MojoExecutionException, MojoFailureException { return; } - ExecutionContext ctx = executionContext(); + List throwables = new ArrayList<>(); + ExecutionContext ctx = executionContext(throwables); + ResultsContainer results = listResults(ctx); RuntimeException firstException = results.getFirstException(); @@ -74,6 +78,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { getLog().error("The recipe produced an error. Please report this to the recipe author."); throw firstException; } + if (!throwables.isEmpty()) { + getLog().warn("The recipe produced " + throwables.size() + " warning(s). Please report this to the recipe author."); + if (!getLog().isDebugEnabled() && !exportDatatables) { + getLog().warn("Run with `--debug` or `-Drewrite.exportDatatables=true` to see all warnings.", throwables.get(0)); + } + } if (results.isNotEmpty()) { Duration estimateTimeSaved = Duration.ZERO; diff --git a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java index 165e4910..ea25810c 100644 --- a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java +++ b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java @@ -125,8 +125,11 @@ protected Environment environment(@Nullable ClassLoader recipeClassLoader) throw return env.build(); } - protected ExecutionContext executionContext() { - return new InMemoryExecutionContext(t -> getLog().debug(t)); + protected ExecutionContext executionContext(List throwables) { + return new InMemoryExecutionContext(t -> { + getLog().debug(t); + throwables.add(t); + }); } protected Path getBuildRoot() { diff --git a/src/main/java/org/openrewrite/maven/AbstractRewriteRunMojo.java b/src/main/java/org/openrewrite/maven/AbstractRewriteRunMojo.java index 3fb46c39..61bac599 100644 --- a/src/main/java/org/openrewrite/maven/AbstractRewriteRunMojo.java +++ b/src/main/java/org/openrewrite/maven/AbstractRewriteRunMojo.java @@ -17,7 +17,6 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; import org.openrewrite.FileAttributes; import org.openrewrite.PrintOutputCapture; @@ -32,6 +31,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; +import java.util.ArrayList; import java.util.List; /** @@ -60,13 +60,22 @@ public void execute() throws MojoExecutionException, MojoFailureException { return; } - ExecutionContext ctx = executionContext(); + List throwables = new ArrayList<>(); + ExecutionContext ctx = executionContext(throwables); + ResultsContainer results = listResults(ctx); - @Nullable RuntimeException firstException = results.getFirstException(); + + RuntimeException firstException = results.getFirstException(); if (firstException != null) { getLog().error("The recipe produced an error. Please report this to the recipe author."); throw firstException; } + if (!throwables.isEmpty()) { + getLog().warn("The recipe produced " + throwables.size() + " warning(s). Please report this to the recipe author."); + if (!getLog().isDebugEnabled() && !exportDatatables) { + getLog().warn("Run with `--debug` or `-Drewrite.exportDatatables=true` to see all warnings.", throwables.get(0)); + } + } if (results.isNotEmpty()) { Duration estimateTimeSaved = Duration.ZERO;