From 4df23d0070e7d74c4d78682b7795f5da34b4a115 Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 21 Jun 2024 17:30:40 +0200 Subject: [PATCH] JS-209 Catch IllegalStateException when saving highlights and cpd tokens (#4746) --- .../javascript/analysis/AnalysisProcessor.java | 17 +++++++++++------ .../analysis/AnalysisProcessorTest.java | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) 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 b12ef1cd526..87fcdc94a76 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 @@ -200,13 +200,18 @@ private void saveHighlights(List highlights) { highlight.location().toTextRange(file), TypeOfText.valueOf(highlight.textType()) ); - } catch (IllegalArgumentException e) { - LOG.warn("Failed to save highlight in {} at {}", file.uri(), highlight.location()); + } catch (RuntimeException e) { + LOG.warn("Failed to create highlight in {} at {}", file.uri(), highlight.location()); LOG.warn("Exception cause", e); // continue processing other highlights } } - highlighting.save(); + try { + highlighting.save(); + } catch (RuntimeException e) { + LOG.warn("Failed to save highlights in {}.", file.uri()); + LOG.warn("Exception cause", e); + } } private void saveHighlightedSymbols(List highlightedSymbols) { @@ -222,7 +227,7 @@ private void saveHighlightedSymbols(List highlightedSymbols) declaration.endLine(), declaration.endCol() ); - } catch (IllegalArgumentException e) { + } catch (RuntimeException e) { LOG.warn("Failed to create symbol declaration in {} at {}", file.uri(), declaration); continue; } @@ -234,7 +239,7 @@ private void saveHighlightedSymbols(List highlightedSymbols) reference.endLine(), reference.endCol() ); - } catch (IllegalArgumentException e) { + } catch (RuntimeException e) { LOG.warn("Failed to create symbol reference in {} at {}", file.uri(), reference); } } @@ -291,7 +296,7 @@ private void saveCpd(List cpdTokens) { newCpdTokens.addToken(cpdToken.location().toTextRange(file), cpdToken.image()); } newCpdTokens.save(); - } catch (IllegalArgumentException e) { + } catch (RuntimeException e) { LOG.warn("Failed to save CPD token in {}. File will not be analyzed for duplications.", file.uri()); 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 eaebaea83f5..ff7f55053f7 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 @@ -45,7 +45,7 @@ void should_not_fail_when_invalid_range() { var response = new AnalysisResponse(null, List.of(), List.of(highlight), List.of(), new Metrics(), List.of(), List.of(), null); processor.processResponse(context, mock(JsTsChecks.class), file, response); assertThat(logTester.logs()) - .contains("Failed to save highlight in " + file.uri() + " at 1:2-1:1"); + .contains("Failed to create highlight in " + file.uri() + " at 1:2-1:1"); } @Test