Skip to content

Commit 7f0905b

Browse files
committed
🔇 Reduce Noise in Logs
When codemodder encounters a text file with a character encoding that it does not support, then it fails to read that file and emits an error level log with a stack trace. This can be very noisy. Instead, we should emit a warn level log with no stack trace when this occurs. warn feels right for a couple of reasons: * this error does not prevent codemodder from completing its analysis; rather, codemodder adds the failed file to its "failed files" list in its CodeTF report. * there will always be cases for which we have no recourse for this problem. Also, we need to include the file path in the warning message. /close #work
1 parent 8be323f commit 7f0905b

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

‎framework/codemodder-base/src/main/java/io/codemodder/DefaultCodemodExecutor.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.codemodder.javaparser.JavaParserCodemodRunner;
1010
import io.codemodder.javaparser.JavaParserFacade;
1111
import java.io.IOException;
12+
import java.nio.charset.MalformedInputException;
1213
import java.nio.file.Files;
1314
import java.nio.file.Path;
1415
import java.util.*;
@@ -130,7 +131,14 @@ public CodeTFResult execute(final List<Path> filePaths) {
130131
}
131132
}
132133

133-
String beforeFileContents = fileCache.get(filePath);
134+
final String beforeFileContents;
135+
try {
136+
beforeFileContents = fileCache.get(filePath);
137+
} catch (final MalformedInputException e) {
138+
log.warn("file uses unsupported character encoding: {}", filePath);
139+
unscannableFiles.add(filePath);
140+
return;
141+
}
134142

135143
Collection<DependencyGAV> deps =
136144
projectProviders.stream()
@@ -163,7 +171,7 @@ public CodeTFResult execute(final List<Path> filePaths) {
163171

164172
} catch (Exception e) {
165173
unscannableFiles.add(filePath);
166-
log.error("Problem scanning file", e);
174+
log.error("Problem scanning file {}", filePath, e);
167175
}
168176
});
169177
}

‎framework/codemodder-base/src/main/java/io/codemodder/javaparser/DefaultJavaParserFacade.java‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import java.nio.file.Path;
99
import java.util.Objects;
1010
import javax.inject.Provider;
11-
import org.slf4j.Logger;
12-
import org.slf4j.LoggerFactory;
1311

1412
final class DefaultJavaParserFacade implements JavaParserFacade {
1513

@@ -31,14 +29,11 @@ public CompilationUnit parseJavaFile(final Path file) throws IOException {
3129

3230
final ParseResult<CompilationUnit> result = parser.parse(file);
3331
if (!result.isSuccessful()) {
34-
logger.error(
35-
"Error while parsing file {} encountered problems: {}", file, result.getProblems());
36-
throw new RuntimeException("can't parse file");
32+
throw new RuntimeException(
33+
"Error while parsing file " + file + " encountered problems: " + result.getProblems());
3734
}
3835
CompilationUnit cu = result.getResult().orElseThrow();
3936
LexicalPreservingPrinter.setup(cu);
4037
return cu;
4138
}
42-
43-
private static final Logger logger = LoggerFactory.getLogger(DefaultJavaParserFacade.class);
4439
}

‎framework/codemodder-base/src/test/java/io/codemodder/javaparser/DefaultJavaParserFacadeTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void it_works_on_good_code() throws IOException {
5454
@Test
5555
void it_fails_loudly_if_cant_parse() throws IOException {
5656
Files.writeString(javaFile, "bad code");
57-
assertThrows(RuntimeException.class, () -> parser.parseJavaFile(javaFile));
57+
assertThrows(JavaParseException.class, () -> parser.parseJavaFile(javaFile));
5858
}
5959

6060
@Test

0 commit comments

Comments
 (0)