diff options
author | David Gageot <david@gageot.net> | 2012-11-07 15:53:42 +0100 |
---|---|---|
committer | David Gageot <david@gageot.net> | 2012-11-07 15:53:42 +0100 |
commit | 8001147ce58dfd68e96017e917b8c6696a7c6127 (patch) | |
tree | d5089d57a987df6b87c1f798eabac1cff790730b /sonar-batch | |
parent | 3798a5c01c847cbbae50da1dd41de1e62b111b51 (diff) | |
download | sonarqube-8001147ce58dfd68e96017e917b8c6696a7c6127.tar.gz sonarqube-8001147ce58dfd68e96017e917b8c6696a7c6127.zip |
SONAR-3895 Improve Http error handling
Diffstat (limited to 'sonar-batch')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java | 24 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java | 15 |
2 files changed, 13 insertions, 26 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java index 0f489826345..837e60d0731 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java @@ -31,8 +31,8 @@ import org.sonar.api.database.DatabaseProperties; import org.sonar.api.utils.SonarException; import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; + +import static org.sonar.api.utils.HttpDownloader.HttpException; /** * @since 3.4 @@ -52,8 +52,8 @@ public class DryRunDatabase implements BatchComponent { private final ProjectReactor reactor; public DryRunDatabase(Settings settings, ServerClient server, TempDirectories tempDirectories, ProjectReactor reactor, - // project reactor must be completely built - ProjectReactorReady reactorReady) { + // project reactor must be completely built + ProjectReactorReady reactorReady) { this.settings = settings; this.server = server; this.tempDirectories = tempDirectories; @@ -76,9 +76,7 @@ public class DryRunDatabase implements BatchComponent { server.download("/batch_bootstrap/db?project=" + projectKey, toFile); } catch (SonarException e) { Throwable rootCause = Throwables.getRootCause(e); - if (rootCause instanceof FileNotFoundException) { - throw new SonarException(String.format("Project [%s] doesn't exist on server", projectKey), e); - } else if ((rootCause instanceof IOException) && (StringUtils.contains(rootCause.getMessage(), "401"))) { + if ((rootCause instanceof HttpException) && (((HttpException) rootCause).getResponseCode() == 401)) { throw new SonarException(String.format("You don't have access rights to project [%s]", projectKey), e); } throw e; @@ -87,11 +85,11 @@ public class DryRunDatabase implements BatchComponent { private void replaceSettings(String databasePath) { settings - .removeProperty("sonar.jdbc.schema") - .setProperty(DatabaseProperties.PROP_DIALECT, DIALECT) - .setProperty(DatabaseProperties.PROP_DRIVER, DRIVER) - .setProperty(DatabaseProperties.PROP_USER, USER) - .setProperty(DatabaseProperties.PROP_PASSWORD, PASSWORD) - .setProperty(DatabaseProperties.PROP_URL, URL + databasePath); + .removeProperty("sonar.jdbc.schema") + .setProperty(DatabaseProperties.PROP_DIALECT, DIALECT) + .setProperty(DatabaseProperties.PROP_DRIVER, DRIVER) + .setProperty(DatabaseProperties.PROP_USER, USER) + .setProperty(DatabaseProperties.PROP_PASSWORD, PASSWORD) + .setProperty(DatabaseProperties.PROP_URL, URL + databasePath); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java index ae4f2826f01..40fef228a4a 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java @@ -28,11 +28,10 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.api.config.Settings; import org.sonar.api.database.DatabaseProperties; +import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.SonarException; import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.doThrow; @@ -88,18 +87,8 @@ public class DryRunDatabaseTest { } @Test - public void should_fail_on_unknown_project() { - doThrow(new SonarException(new FileNotFoundException())).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile); - - thrown.expect(SonarException.class); - thrown.expectMessage("Project [group:project] doesn't exist on server"); - - dryRunDatabase.start(); - } - - @Test public void should_fail_on_invalid_role() { - doThrow(new SonarException(new IOException("HTTP 401"))).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile); + doThrow(new SonarException(new HttpDownloader.HttpException(null, 401))).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile); thrown.expect(SonarException.class); thrown.expectMessage("You don't have access rights to project [group:project]"); |