Skip to content

Fix NPE when calling ParsingException constructors with null reason #839

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

Merged
merged 2 commits into from
Dec 12, 2022
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
2 changes: 1 addition & 1 deletion language-api/src/main/java/de/jplag/ParsingException.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private ParsingException(String message) {
private static String constructMessage(File file, String reason) {
StringBuilder messageBuilder = new StringBuilder();
messageBuilder.append("failed to parse '%s'".formatted(file));
if (!reason.isEmpty() && !reason.isBlank()) {
if (reason != null && !reason.isBlank()) {
messageBuilder.append(" with reason: %s".formatted(reason));
}
return messageBuilder.toString();
Expand Down
23 changes: 23 additions & 0 deletions language-api/src/test/java/de/jplag/ParsingExceptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.jplag;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import java.io.File;

import org.junit.jupiter.api.Test;

class ParsingExceptionTest {

@Test
void testParsingExceptionAcceptsNullReason() {
// placeholder exception to have a non-null argument
File file = new File("myFile");
// placeholder exception to have a non-null argument
Exception exception = new RuntimeException();

assertDoesNotThrow(() -> new ParsingException(file));
assertDoesNotThrow(() -> new ParsingException(file, (String) null));
assertDoesNotThrow(() -> new ParsingException(file, exception));
assertDoesNotThrow(() -> new ParsingException(file, null, exception));
}
}