From a31a9d6f9b2368bbd1a4d53751a893e0adc38f87 Mon Sep 17 00:00:00 2001 From: yassin-kammoun-sonarsource Date: Tue, 26 Nov 2024 13:06:29 +0100 Subject: [PATCH 1/2] JS-367 Survive runtime failures on saving malformed issues --- .../javascript/analysis/AnalysisProcessor.java | 7 ++++++- .../analysis/AnalysisProcessorTest.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) 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..bdf584a92e7 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("Reason: {}", e.getMessage()); + } } } 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"); + } } From bc86f374b401d3d6d09a2b49cf46c05e85ad9da1 Mon Sep 17 00:00:00 2001 From: Yassin Kammoun <52890329+yassin-kammoun-sonarsource@users.noreply.github.com> Date: Tue, 26 Nov 2024 16:26:53 +0100 Subject: [PATCH 2/2] Update sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/analysis/AnalysisProcessor.java Co-authored-by: Tibor Blenessy --- .../sonar/plugins/javascript/analysis/AnalysisProcessor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 bdf584a92e7..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 @@ -189,7 +189,7 @@ private void saveIssues(List issues) { saveIssue(issue); } catch (RuntimeException e) { LOG.warn("Failed to save issue in {} at line {}", file.uri(), issue.line()); - LOG.warn("Reason: {}", e.getMessage()); + LOG.warn("Exception cause", e); } } }