diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2018-06-11 10:49:18 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-06-11 20:20:49 +0200 |
commit | 618039ace997ff9fda4735988edd19233159ac9b (patch) | |
tree | 568d2df4e6d5375b7942b1e54cead4caa6e56fc1 /sonar-scanner-engine | |
parent | 3c1453a726a753e9f8e15a43d70d7e7ee1d835a8 (diff) | |
download | sonarqube-618039ace997ff9fda4735988edd19233159ac9b.tar.gz sonarqube-618039ace997ff9fda4735988edd19233159ac9b.zip |
SONAR-10464 Improve error message when passing a wrong organization during the analysis
Diffstat (limited to 'sonar-scanner-engine')
2 files changed, 34 insertions, 7 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultQualityProfileLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultQualityProfileLoader.java index 0b2be6f39e9..8d68be56d4f 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultQualityProfileLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultQualityProfileLoader.java @@ -26,6 +26,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.BinaryOperator; +import java.util.function.Supplier; import javax.annotation.Nullable; import org.sonar.api.utils.MessageException; import org.sonar.scanner.bootstrap.ScannerWsClient; @@ -33,6 +34,7 @@ import org.sonar.scanner.scan.ScanProperties; import org.sonarqube.ws.Qualityprofiles.SearchWsResponse; import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile; import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.HttpException; import static java.util.function.Function.identity; import static java.util.stream.Collectors.toMap; @@ -52,16 +54,31 @@ public class DefaultQualityProfileLoader implements QualityProfileLoader { @Override public List<QualityProfile> loadDefault(@Nullable String profileName) { StringBuilder url = new StringBuilder(WS_URL + "?defaults=true"); - return loadAndOverrideIfNeeded(profileName, url); + return handleErrors(profileName, url, () -> "Failed to load the default quality profiles"); } @Override public List<QualityProfile> load(String projectKey, @Nullable String profileName) { StringBuilder url = new StringBuilder(WS_URL + "?projectKey=").append(encodeForUrl(projectKey)); - return loadAndOverrideIfNeeded(profileName, url); + return handleErrors(profileName, url, () -> String.format("Failed to load the quality profiles of project '%s'", projectKey)); } - private List<QualityProfile> loadAndOverrideIfNeeded(@Nullable String profileName, StringBuilder url) { + private List<QualityProfile> handleErrors(@Nullable String profileName, StringBuilder url, Supplier<String> errorMsg) { + try { + return loadAndOverrideIfNeeded(profileName, url); + } catch (HttpException e) { + if (e.code() == 404) { + throw MessageException.of(errorMsg.get() + ": " + ScannerWsClient.tryParseAsJsonError(e.content())); + } + throw new IllegalStateException(errorMsg.get(), e); + } catch (MessageException e) { + throw e; + } catch (Exception e) { + throw new IllegalStateException(errorMsg.get(), e); + } + } + + private List<QualityProfile> loadAndOverrideIfNeeded(@Nullable String profileName, StringBuilder url) throws IOException { properties.organizationKey().ifPresent(k -> url.append("&organization=").append(encodeForUrl(k))); Map<String, QualityProfile> result = call(url.toString()); @@ -78,16 +95,13 @@ public class DefaultQualityProfileLoader implements QualityProfileLoader { return new ArrayList<>(result.values()); } - private Map<String, QualityProfile> call(String url) { + private Map<String, QualityProfile> call(String url) throws IOException { GetRequest getRequest = new GetRequest(url); try (InputStream is = wsClient.call(getRequest).contentStream()) { SearchWsResponse profiles = SearchWsResponse.parseFrom(is); List<QualityProfile> profilesList = profiles.getProfilesList(); return profilesList.stream().collect(toMap(QualityProfile::getLanguage, identity(), throwingMerger(), LinkedHashMap::new)); - } catch (IOException e) { - throw new IllegalStateException("Failed to load quality profiles", e); } - } private static <T> BinaryOperator<T> throwingMerger() { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest.java index b1786ef3ad9..2e7f4857f8b 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultQualityProfileLoaderTest.java @@ -33,6 +33,7 @@ import org.sonar.scanner.bootstrap.ScannerWsClient; import org.sonar.scanner.scan.ScanProperties; import org.sonarqube.ws.Qualityprofiles; import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile; +import org.sonarqube.ws.client.HttpException; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -116,6 +117,18 @@ public class DefaultQualityProfileLoaderTest { verifyNoMoreInteractions(wsClient); } + @Test + public void load_throws_MessageException_if_organization_is_not_found() throws IOException { + HttpException e = new HttpException("", 404, "{\"errors\":[{\"msg\":\"No organization with key 'myorg'\"}]}"); + WsTestUtil.mockException(wsClient, e); + + exception.expect(MessageException.class); + exception.expectMessage("Failed to load the quality profiles of project 'project': No organization with key 'myorg'"); + + underTest.load("project", null); + verifyNoMoreInteractions(wsClient); + } + private void verifyCalledPath(String expectedPath) { WsTestUtil.verifyCall(wsClient, expectedPath); } |