From 3e89b4a9d96d684518d8058b3b88ed6c07e52cd1 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Fri, 15 Nov 2024 12:36:00 +0100 Subject: [PATCH] Also report `DeserializationError`s in `FindParseFailures` recipe --- .../org/openrewrite/FindParseFailures.java | 80 ++++++++++++------- 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/rewrite-core/src/main/java/org/openrewrite/FindParseFailures.java b/rewrite-core/src/main/java/org/openrewrite/FindParseFailures.java index 6451ab0aaba..4e9168b6439 100644 --- a/rewrite-core/src/main/java/org/openrewrite/FindParseFailures.java +++ b/rewrite-core/src/main/java/org/openrewrite/FindParseFailures.java @@ -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; @@ -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; @@ -67,33 +68,54 @@ public TreeVisitor 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()); } }; }