From 55e82b7148a771d4651bd983f65e9d5a6a30a6b1 Mon Sep 17 00:00:00 2001 From: Jacek Date: Thu, 16 Sep 2021 11:56:38 +0200 Subject: [PATCH] SONAR-13337 Improve ES error message in batch requests to guide user to check ES logs --- .../org/sonar/server/es/IndexingListener.java | 7 ++++++- .../es/FailOnErrorIndexingListenerTest.java | 20 +++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/IndexingListener.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/IndexingListener.java index 095451c7f7d..6d9b7972a07 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/IndexingListener.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/es/IndexingListener.java @@ -21,6 +21,8 @@ package org.sonar.server.es; import java.util.List; +import static java.lang.String.format; + public interface IndexingListener { void onSuccess(List docIds); @@ -36,7 +38,10 @@ public interface IndexingListener { @Override public void onFinish(IndexingResult result) { if (result.getFailures() > 0) { - throw new IllegalStateException(String.format("Unrecoverable indexation failures: %d errors among %d requests", result.getFailures(), result.getTotal())); + throw new IllegalStateException( + format("Unrecoverable indexation failures: %d errors among %d requests. Check Elasticsearch logs for further details.", + result.getFailures(), + result.getTotal())); } } }; diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/es/FailOnErrorIndexingListenerTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/es/FailOnErrorIndexingListenerTest.java index a693f0bb178..631ba9df6c3 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/es/FailOnErrorIndexingListenerTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/es/FailOnErrorIndexingListenerTest.java @@ -19,14 +19,13 @@ */ package org.sonar.server.es; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; -public class FailOnErrorIndexingListenerTest { +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.sonar.server.es.IndexingListener.FAIL_ON_ERROR; - @Rule - public ExpectedException expectedException = ExpectedException.none(); +public class FailOnErrorIndexingListenerTest { @Test public void onFinish_must_throw_ISE_when_an_error_is_present() { @@ -34,10 +33,10 @@ public class FailOnErrorIndexingListenerTest { indexingResult.incrementRequests(); - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Unrecoverable indexation failures"); - - IndexingListener.FAIL_ON_ERROR.onFinish(indexingResult); + assertThatThrownBy(() -> FAIL_ON_ERROR.onFinish(indexingResult)) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Unrecoverable indexation failures: 1 errors among 1 requests. " + + "Check Elasticsearch logs for further details."); } @Test @@ -47,6 +46,7 @@ public class FailOnErrorIndexingListenerTest { indexingResult.incrementRequests(); indexingResult.incrementSuccess(); - IndexingListener.FAIL_ON_ERROR.onFinish(indexingResult); + assertThatCode(() -> FAIL_ON_ERROR.onFinish(indexingResult)) + .doesNotThrowAnyException(); } } -- 2.39.5