Skip to content

Commit

Permalink
Also report DeserializationErrors in FindParseFailures recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Nov 15, 2024
1 parent 5ef0ef1 commit 3e89b4a
Showing 1 changed file with 51 additions and 29 deletions.
80 changes: 51 additions & 29 deletions rewrite-core/src/main/java/org/openrewrite/FindParseFailures.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.marker.DeserializationError;
import org.openrewrite.marker.Markup;
import org.openrewrite.table.ParseFailures;

Expand All @@ -35,9 +36,9 @@ public class FindParseFailures extends Recipe {
Integer maxSnippetLength;

@Option(displayName = "Parser type",
description = "Only display failures from parsers with this fully qualified name.",
description = "Only display failures from parsers with this simple name.",
required = false,
example = "org.openrewrite.yaml.YamlParser")
example = "YamlParser")
@Nullable
String parserType;

Expand Down Expand Up @@ -67,33 +68,54 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public Tree postVisit(Tree tree, ExecutionContext ctx) {
return tree.getMarkers().findFirst(ParseExceptionResult.class)
.map(exceptionResult -> {
if (parserType != null && !Objects.equals(exceptionResult.getParserType(), parserType)) {
return tree;
}

if (stackTrace != null && !exceptionResult.getMessage().contains(stackTrace)) {
return tree;
}

String snippet = tree instanceof SourceFile ? null : tree.printTrimmed(getCursor().getParentTreeCursor());
if (snippet != null && maxSnippetLength != null && snippet.length() > maxSnippetLength) {
snippet = snippet.substring(0, maxSnippetLength);
}

failures.insertRow(ctx, new ParseFailures.Row(
exceptionResult.getParserType(),
(tree instanceof SourceFile ? (SourceFile) tree : getCursor().firstEnclosingOrThrow(SourceFile.class))
.getSourcePath().toString(),
exceptionResult.getExceptionType(),
exceptionResult.getTreeType(),
snippet,
exceptionResult.getMessage()
));

return Markup.info(tree, exceptionResult.getMessage());
})
.orElse(tree);
.map(exceptionResult -> report(tree, exceptionResult, ctx))
.orElse(tree.getMarkers().findFirst(DeserializationError.class)
.map(error -> report(tree, error, ctx))
.orElse(tree)
);
}

private Tree report(Tree tree, DeserializationError error, ExecutionContext ctx) {
if (stackTrace != null && !error.getDetail().contains(stackTrace)) {
return tree;
}

failures.insertRow(ctx, new ParseFailures.Row(
"Unknown",
(tree instanceof SourceFile ? (SourceFile) tree : getCursor().firstEnclosingOrThrow(SourceFile.class))
.getSourcePath().toString(),
"DeserializationError",
null,
null,
error.getDetail()
));

return Markup.info(tree, error.getMessage());
}

private Tree report(Tree tree, ParseExceptionResult exceptionResult, ExecutionContext ctx) {
if (parserType != null && !Objects.equals(exceptionResult.getParserType(), parserType)) {
return tree;
} else if (stackTrace != null && !exceptionResult.getMessage().contains(stackTrace)) {
return tree;
}

String snippet = tree instanceof SourceFile ? null : tree.printTrimmed(getCursor().getParentTreeCursor());
if (snippet != null && maxSnippetLength != null && snippet.length() > maxSnippetLength) {
snippet = snippet.substring(0, maxSnippetLength);
}

failures.insertRow(ctx, new ParseFailures.Row(
exceptionResult.getParserType(),
(tree instanceof SourceFile ? (SourceFile) tree : getCursor().firstEnclosingOrThrow(SourceFile.class))
.getSourcePath().toString(),
exceptionResult.getExceptionType(),
exceptionResult.getTreeType(),
snippet,
exceptionResult.getMessage()
));

return Markup.info(tree, exceptionResult.getMessage());
}
};
}
Expand Down

0 comments on commit 3e89b4a

Please sign in to comment.