Skip to content

Commit

Permalink
Ignore runtime errors when saving CPD tokens (#4654)
Browse files Browse the repository at this point in the history
  • Loading branch information
saberduck authored Apr 5, 2024
1 parent abe7b44 commit 0e5e923
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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");
}
}

0 comments on commit 0e5e923

Please sign in to comment.