Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding visited-file details to unexpected exceptions #2664

Merged
merged 2 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion rewrite-core/src/main/java/org/openrewrite/TreeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public T visit(@Nullable Tree tree, P p) {
throw e;
}

throw new RecipeRunException(e, getCursor());
throw new RecipeRunException(e, getCursor(), describeLocation(getCursor()));
}

//noinspection unchecked
Expand Down Expand Up @@ -410,4 +410,13 @@ public <R extends Tree, V extends TreeVisitor<R, P>> V adapt(Class<? extends V>
}
return TreeVisitorAdapter.adapt(this, adaptTo);
}

@Nullable
protected String describeLocation(Cursor cursor) {
SourceFile sourceFile = cursor.firstEnclosing(SourceFile.class);
if (sourceFile == null) {
return null;
}
return sourceFile.getSourcePath().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public FindRecipeRunException(RecipeRunException rre) {
@Override
public Tree preVisit(Tree tree, Integer integer) {
if (tree == nearestTree) {
return Markup.error(tree, vt.getCause());
return Markup.error(tree, vt);
}
return tree;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ public class RecipeRunException extends RuntimeException {
private final Cursor cursor;

public RecipeRunException(Throwable cause, @Nullable Cursor cursor) {
super(cause);
this(cause, cursor, null);
}

public RecipeRunException(Throwable cause, @Nullable Cursor cursor, @Nullable String visitedLocation) {
super(message(visitedLocation, cause), cause);
this.cursor = cursor;
}

@Nullable
private static String message(@Nullable String visitedLocation, Throwable cause) {
return visitedLocation == null ? null
: String.format("Exception while visiting project file '%s', caused by: %s", visitedLocation, cause);
}
}
17 changes: 13 additions & 4 deletions rewrite-core/src/main/java/org/openrewrite/marker/Markup.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openrewrite.RecipeScheduler;
import org.openrewrite.Tree;
import org.openrewrite.internal.ExceptionUtils;
import org.openrewrite.internal.RecipeRunException;
import org.openrewrite.internal.lang.NonNull;
import org.openrewrite.internal.lang.Nullable;

Expand Down Expand Up @@ -84,13 +85,17 @@ class Error implements Markup {

@Override
public String getMessage() {
return exception.getMessage();
return getCause().getMessage();
}

@Override
@NonNull
public String getDetail() {
return ExceptionUtils.sanitizeStackTrace(exception, RecipeScheduler.class);
return ExceptionUtils.sanitizeStackTrace(getCause(), RecipeScheduler.class);
}

private Throwable getCause() {
return exception instanceof RecipeRunException ? exception.getCause() : exception;
}

@Override
Expand All @@ -115,13 +120,17 @@ class Warn implements Markup {

@Override
public String getMessage() {
return exception.getMessage();
return getCause().getMessage();
}

@Override
@NonNull
public String getDetail() {
return ExceptionUtils.sanitizeStackTrace(exception, RecipeScheduler.class);
return ExceptionUtils.sanitizeStackTrace(getCause(), RecipeScheduler.class);
}

private Throwable getCause() {
return exception instanceof RecipeRunException ? exception.getCause() : exception;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ void exceptionsCauseResult() {
spec -> spec
.executionContext(new InMemoryExecutionContext())
.recipe(new BoomRecipe())
.afterRecipe(run -> assertThat(run.getResults().get(0).getRecipeErrors()).isNotEmpty()),
.afterRecipe(run -> assertThat(run.getResults().get(0).getRecipeErrors())
.singleElement()
.satisfies(t -> assertThat(t.getMessage()).isEqualTo("Exception while visiting project file 'file.txt', caused by: org.openrewrite.BoomException: boom"))
),
text(
"hello",
"~~(boom)~~>hello"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.ExecutionContext;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.tree.J;
import org.openrewrite.test.RewriteTest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.test.RewriteTest.toRecipe;

Expand Down Expand Up @@ -74,4 +76,38 @@ void removeMethod() {}
)
);
}

@Test
void thrownExceptionsAreSpecific() {
rewriteRun(
spec -> spec
.executionContext(new InMemoryExecutionContext())
.afterRecipe(run -> assertThat(run.getResults().get(0).getRecipeErrors())
.singleElement()
.satisfies(t -> assertThat(t.getMessage()).containsSubsequence("A.java", "A", "allTheThings"))
)
.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.Literal visitLiteral(final J.Literal literal, final ExecutionContext executionContext) {
throw new IllegalStateException("boom");
}
})),
java(
"""
class A {
void allTheThings() {
String var = "qwe";
}
}
""",
"""
class A {
void allTheThings() {
String var = /*~~(boom)~~>*/"qwe";
}
}
"""
)
);
}
}
17 changes: 17 additions & 0 deletions rewrite-java/src/main/java/org/openrewrite/java/JavaVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -1379,4 +1380,20 @@ protected boolean isInSameNameScope(Cursor base, Cursor child) {
protected boolean isInSameNameScope(Cursor child) {
return isInSameNameScope(getCursor(), child);
}

@Override
protected @Nullable String describeLocation(Cursor cursor) {
List<String> namedElements = new ArrayList<>();
for (Iterator<Object> it = cursor.getPath(); it.hasNext(); ) {
final Object tree = it.next();
if (tree instanceof J.ClassDeclaration) {
namedElements.add(0, ((J.ClassDeclaration) tree).getSimpleName());
} else if (tree instanceof J.MethodDeclaration) {
namedElements.add(0, ((J.MethodDeclaration) tree).getSimpleName());
}
}
String location = String.join(".", namedElements);
String filename = super.describeLocation(cursor);
return "".equals(location) ? filename : String.format("%s (in %s)", filename, location);
}
}