Skip to content

Fix NPEs in PrintStackTraceToLogError, CompleteExceptionLogging - #310

Merged
greg-at-moderne merged 2 commits into
mainfrom
greg-npes-throwable-subclasses-cherry-pick
Oct 15, 2026
Merged

greg-at-moderne merged 2 commits into
mainfrom
greg-npes-throwable-subclasses-cherry-pick

Conversation

@greg-at-moderne

Copy link
Copy Markdown
Contributor

What's changed?

Additional guards added in two recipes PrintStackTraceToLogError and CompleteExceptionLogging.

Basically the recipes assumed that calls to .getMessage() or similar methods from Throwable always came qualified, i.e. e.getMessage(). That's not necessarily the case in a Throwable subclass.

What's your motivation?

Otherwise they fail with NPEs like:

java.lang.NullPointerException: Cannot invoke "org.openrewrite.java.tree.J.withPrefix(org.openrewrite.java.tree.Space)" because "parameter" is null
	at org.openrewrite.java.internal.template.Substitutions.substituteTypedPattern(Substitutions.java:157)
	at org.openrewrite.java.internal.template.Substitutions.substitute(Substitutions.java:75)
	at org.openrewrite.java.JavaTemplate.apply(JavaTemplate.java:110)
	at org.openrewrite.java.logging.PrintStackTraceToLogError$1.replaceMethodInvocation(PrintStackTraceToLogError.java:113)
	at org.openrewrite.java.logging.PrintStackTraceToLogError$1.visitMethodInvocation(PrintStackTraceToLogError.java:93)

or

java.lang.NullPointerException
	at java.base/java.util.Objects.requireNonNull(Objects.java:233)
	at java.base/java.util.Optional.of(Optional.java:113)
	at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:647)
	at org.openrewrite.java.logging.slf4j.CompleteExceptionLogging$1.visitMethodInvocation(CompleteExceptionLogging.java:143)

@greg-at-moderne greg-at-moderne self-assigned this Oct 15, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Oct 15, 2026
@greg-at-moderne
greg-at-moderne marked this pull request as ready for review October 15, 2026 10:57
@greg-at-moderne
greg-at-moderne merged commit 4659fb4 into main Oct 15, 2026
1 check passed
@greg-at-moderne
greg-at-moderne deleted the greg-npes-throwable-subclasses-cherry-pick branch October 15, 2026 10:57
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant