Skip to content

Commit

Permalink
Report first Throwable caught in InMemoryExecutionContext.onError (
Browse files Browse the repository at this point in the history
…#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>
  • Loading branch information
timtebeek and github-actions[bot] authored Feb 5, 2025
1 parent b09110a commit f2c4b2c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -66,14 +68,22 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}

ExecutionContext ctx = executionContext();
List<Throwable> throwables = new ArrayList<>();
ExecutionContext ctx = executionContext(throwables);

ResultsContainer results = listResults(ctx);

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;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Throwable> throwables) {
return new InMemoryExecutionContext(t -> {
getLog().debug(t);
throwables.add(t);
});
}

protected Path getBuildRoot() {
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/openrewrite/maven/AbstractRewriteRunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -60,13 +60,22 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}

ExecutionContext ctx = executionContext();
List<Throwable> 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;
Expand Down

0 comments on commit f2c4b2c

Please sign in to comment.