diff --git a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/AnalysisProcessor.java b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/AnalysisProcessor.java index cc7a5d63fae..c8e4d30d89a 100644 --- a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/AnalysisProcessor.java +++ b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/AnalysisProcessor.java @@ -185,7 +185,12 @@ private void saveIssues(List issues) { file, issue.line() ); - saveIssue(issue); + try { + saveIssue(issue); + } catch (RuntimeException e) { + LOG.warn("Failed to save issue in {} at line {}", file.uri(), issue.line()); + LOG.warn("Exception cause", e); + } } } diff --git a/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/analysis/AnalysisProcessorTest.java b/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/analysis/AnalysisProcessorTest.java index 6bbf4b74624..6ab828b2b15 100644 --- a/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/analysis/AnalysisProcessorTest.java +++ b/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/analysis/AnalysisProcessorTest.java @@ -35,6 +35,7 @@ import org.sonar.plugins.javascript.bridge.BridgeServer.CpdToken; import org.sonar.plugins.javascript.bridge.BridgeServer.Highlight; import org.sonar.plugins.javascript.bridge.BridgeServer.HighlightedSymbol; +import org.sonar.plugins.javascript.bridge.BridgeServer.Issue; import org.sonar.plugins.javascript.bridge.BridgeServer.Location; import org.sonar.plugins.javascript.bridge.BridgeServer.Metrics; @@ -107,4 +108,21 @@ void should_not_fail_when_invalid_cpd() { assertThat(logTester.logs()) .contains("Failed to save CPD token in " + file.uri() + ". File will not be analyzed for duplications."); } + + @Test + void should_not_fail_when_invalid_issue() { + 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 issue = new Issue(2, 1, 1, 2, "message", "ruleId", List.of(), 3.14, List.of()); // invalid location startLine > endLine + var response = new AnalysisResponse(null, List.of(issue), List.of(), List.of(), new Metrics(), List.of(), List.of(), null); + processor.processResponse(context, mock(JsTsChecks.class), file, response); + assertThat(logTester.logs()) + .contains("Failed to save issue in " + file.uri() + " at line 2"); + } }