diff options
author | 2016-01-05 22:26:18 +0000 | |
---|---|---|
committer | 2016-01-07 13:45:18 +0000 | |
commit | e1de068273cd8e72f34410526f27c779a8b69f30 (patch) | |
tree | 5aa10eccbca8d281b6d267f2430888bcf0aea3e4 /src/main/java/com/google/devtools/build/lib/bazel | |
parent | 00bbb18234d25c94ff0eb1c8dc7443f91fdd7203 (diff) |
Improve error message when download fails
Instead of just showing the URL that the file failed to be fetched from, it now
displays the response code and content (if possible).
Fixes #732.
--
MOS_MIGRATED_REVID=111450650
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/bazel')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/bazel/repository/HttpDownloader.java | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/HttpDownloader.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/HttpDownloader.java index 3f1495012f..e75c7daeb5 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/HttpDownloader.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/HttpDownloader.java @@ -18,6 +18,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; import com.google.common.hash.Hasher; import com.google.common.hash.Hashing; +import com.google.common.io.ByteStreams; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.EventHandler; import com.google.devtools.build.lib.packages.AggregatingAttributeMapper; @@ -35,6 +36,7 @@ import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; @@ -110,13 +112,13 @@ public class HttpDownloader { AtomicInteger totalBytes = new AtomicInteger(0); final ScheduledFuture<?> loggerHandle = getLoggerHandle(totalBytes); - try (OutputStream outputStream = destination.getOutputStream()) { - InputStream in = getInputStream(url); + try (OutputStream out = destination.getOutputStream(); + InputStream in = getInputStream(url)) { int read; byte[] buf = new byte[BUFFER_SIZE]; while ((read = in.read(buf)) > 0) { totalBytes.addAndGet(read); - outputStream.write(buf, 0, read); + out.write(buf, 0, read); } } catch (IOException e) { throw new IOException( @@ -183,7 +185,13 @@ public class HttpDownloader { createProxyIfNeeded(url.getProtocol())); connection.setInstanceFollowRedirects(true); connection.connect(); - return connection.getInputStream(); + if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { + return connection.getInputStream(); + } + + InputStream errorStream = connection.getErrorStream(); + throw new IOException(connection.getResponseCode() + ": " + + new String(ByteStreams.toByteArray(errorStream), StandardCharsets.UTF_8)); } private static Proxy createProxyIfNeeded(String protocol) throws IOException { |