From: Duarte Meneses Date: Tue, 24 Nov 2015 13:16:07 +0000 (+0100) Subject: SONAR-7037 Improve logging on authentication failure X-Git-Tag: 5.3-RC1~186 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5634bf689509aed3c48877469d4549ab7b5141a4;p=sonarqube.git SONAR-7037 Improve logging on authentication failure --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java index 11bf530e8b3..b31741f1115 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java @@ -19,8 +19,9 @@ */ package org.sonar.batch.repository; -import org.sonar.api.utils.HttpDownloader.HttpException; +import com.google.common.base.Throwables; +import org.sonar.api.utils.HttpDownloader.HttpException; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; @@ -59,7 +60,7 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad fromCache.setValue(result.isFromCache()); } return processStream(result.get(), projectKey); - } catch (IllegalStateException e) { + } catch (RuntimeException e) { if (shouldThrow(e)) { throw e; } @@ -81,9 +82,11 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad } private static boolean shouldThrow(Exception e) { - if (e.getCause() != null && e.getCause() instanceof HttpException) { - HttpException http = (HttpException) e.getCause(); - return http.getResponseCode() != 404; + for (Throwable t : Throwables.getCausalChain(e)) { + if (t instanceof HttpException) { + HttpException http = (HttpException) t; + return http.getResponseCode() != 404; + } } return false; diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java index 2641fc2b536..b8c9012399a 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.repository; +import org.sonar.api.utils.MessageException; + import com.google.common.io.Resources; import java.io.ByteArrayInputStream; @@ -81,6 +83,17 @@ public class DefaultProjectRepositoriesLoaderTest { when(wsLoader.loadStream(anyString())).thenThrow(e); loader.load(PROJECT_KEY, false, null); } + + @Test + public void failFastHttpErrorMessageException() { + thrown.expect(MessageException.class); + thrown.expectMessage("http error"); + + HttpException http = new HttpException(URI.create("uri"), 403); + MessageException e = MessageException.of("http error", http); + when(wsLoader.loadStream(anyString())).thenThrow(e); + loader.load(PROJECT_KEY, false, null); + } @Test public void passIssuesModeParameter() {