]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9540 do not log MessageException thrown during task processing
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 12 Jul 2017 09:21:28 +0000 (11:21 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 21 Jul 2017 10:22:10 +0000 (12:22 +0200)
server/sonar-ce/src/main/java/org/sonar/ce/taskprocessor/CeWorkerImpl.java
server/sonar-ce/src/test/java/org/sonar/ce/taskprocessor/CeWorkerImplTest.java

index cbcabd72a84ca9ee43f4ea66569d53ac6029f277..b5db945cb9690a7adfc0468cd1e766d0b822dfa0 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.ce.taskprocessor;
 import java.util.Optional;
 import java.util.function.Supplier;
 import javax.annotation.Nullable;
+import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.ce.log.CeLogging;
@@ -133,6 +134,8 @@ public class CeWorkerImpl implements CeWorker {
         LOG.error("No CeTaskProcessor is defined for task of type {}. Plugin configuration may have changed", task.getType());
         status = CeActivityDto.Status.FAILED;
       }
+    } catch (MessageException e) {
+      error = e;
     } catch (Throwable e) {
       LOG.error(format("Failed to execute task %s", task.getUuid()), e);
       error = e;
index b75cd6887766c0c241655146e3221977edf4fcb4..dfef725387289b82d7c5176f66f48edf74ad3309 100644 (file)
@@ -32,6 +32,7 @@ import org.junit.rules.ExpectedException;
 import org.mockito.ArgumentCaptor;
 import org.mockito.InOrder;
 import org.mockito.Mockito;
+import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.log.LogTester;
 import org.sonar.api.utils.log.LoggerLevel;
 import org.sonar.ce.log.CeLogging;
@@ -327,6 +328,33 @@ public class CeWorkerImplTest {
     newThread.join();
   }
 
+  @Test
+  public void log_error_when_task_fails_with_not_MessageException() throws Exception {
+    CeTask ceTask = createCeTask("FooBar");
+    when(queue.peek(anyString())).thenReturn(Optional.of(ceTask));
+    taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
+    makeTaskProcessorFail(ceTask);
+
+    underTest.call();
+    List<String> logs = logTester.logs(LoggerLevel.ERROR);
+    assertThat(logs).hasSize(2);
+    assertThat(logs.get(0)).isEqualTo("Failed to execute task " + ceTask.getUuid());
+    assertThat(logs.get(1)).contains(" | submitter=FooBar | time=");
+  }
+
+  @Test
+  public void do_no_log_error_when_task_fails_with_MessageException() throws Exception {
+    CeTask ceTask = createCeTask("FooBar");
+    when(queue.peek(anyString())).thenReturn(Optional.of(ceTask));
+    taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
+    makeTaskProcessorFail(ceTask, MessageException.of("simulate MessageException thrown by TaskProcessor#process"));
+
+    underTest.call();
+    List<String> logs = logTester.logs(LoggerLevel.ERROR);
+    assertThat(logs).hasSize(1);
+    assertThat(logs.get(0)).contains(" | submitter=FooBar | time=");
+  }
+
   private Thread createThreadNameVerifyingThread(String threadName) {
     return new Thread(() -> {
       verifyUnchangedThreadName(threadName);
@@ -358,8 +386,11 @@ public class CeWorkerImplTest {
   }
 
   private IllegalStateException makeTaskProcessorFail(CeTask task) {
-    IllegalStateException error = new IllegalStateException("simulate exception thrown by TaskProcessor#process");
-    doThrow(error).when(taskProcessor).process(task);
-    return error;
+    return makeTaskProcessorFail(task, new IllegalStateException("simulate exception thrown by TaskProcessor#process"));
+  }
+
+  private <T extends Throwable> T makeTaskProcessorFail(CeTask task, T t) {
+    doThrow(t).when(taskProcessor).process(task);
+    return t;
   }
 }