Skip to content
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

Ignore runtime errors when saving CPD tokens #4654

Merged
merged 1 commit into from
Apr 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,13 @@ private void saveCpd(BridgeServer.CpdToken[] cpdTokens) {
}
NewCpdTokens newCpdTokens = context.newCpdTokens().onFile(file);
for (BridgeServer.CpdToken cpdToken : cpdTokens) {
newCpdTokens.addToken(cpdToken.location.toTextRange(file), cpdToken.image);
try {
newCpdTokens.addToken(cpdToken.location.toTextRange(file), cpdToken.image);
} catch (IllegalArgumentException e) {
LOG.warn("Failed to save CPD token in {} at {}", file.uri(), cpdToken.location);
LOG.warn("Exception cause", e);
// continue processing other tokens
}
}
newCpdTokens.save();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,23 @@ void should_not_fail_when_invalid_symbol() {
assertThat(logTester.logs())
.contains("Failed to create symbol reference in " + file.uri() + " at 2:2-2:1");
}

@Test
void should_not_fail_when_invalid_cpd() {
var fileLinesContextFactory = mock(FileLinesContextFactory.class);
when(fileLinesContextFactory.createFor(any())).thenReturn(mock(FileLinesContext.class));
var processor = new AnalysisProcessor(mock(NoSonarFilter.class), fileLinesContextFactory);
var context = SensorContextTester.create(baseDir);
var file = TestInputFileBuilder
.create("moduleKey", "file.js")
.setContents("var x = 1;")
.build();
var response = new BridgeServer.AnalysisResponse();
var cpd = new BridgeServer.CpdToken();
cpd.location = new BridgeServer.Location(1, 2, 1, 1); // invalid range startCol > endCol
response.cpdTokens = new BridgeServer.CpdToken[] { cpd };
processor.processResponse(context, mock(JsTsChecks.class), file, response);
assertThat(logTester.logs())
.contains("Failed to save CPD token in " + file.uri() + " at 1:2-1:1");
}
}
Loading