From 284bca6f1e211b5a0a4e202c7401bb09aa584742 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Mon, 22 Dec 2014 11:30:20 +0100 Subject: [PATCH] extract createErrorMessage (IDEA-CR-1104) --- .../templates/github/DownloadUtil.java | 11 +++------- .../com/intellij/util/io/HttpRequests.java | 20 +++++++++++-------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/platform/templates/github/DownloadUtil.java b/platform/lang-impl/src/com/intellij/platform/templates/github/DownloadUtil.java index 5199133d7259..792fdc40d45e 100644 --- a/platform/lang-impl/src/com/intellij/platform/templates/github/DownloadUtil.java +++ b/platform/lang-impl/src/com/intellij/platform/templates/github/DownloadUtil.java @@ -17,7 +17,6 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.net.HttpURLConnection; import java.util.Locale; import java.util.concurrent.Callable; @@ -164,7 +163,7 @@ public class DownloadUtil { } private static void download(@Nullable final ProgressIndicator progress, - @NotNull final String location, + @NotNull String location, @NotNull final OutputStream output) throws IOException { final String originalText = progress != null ? progress.getText() : null; substituteContentLength(progress, originalText, -1); @@ -184,18 +183,14 @@ public class DownloadUtil { NetUtils.copyStreamContent(progress, request.getInputStream(), output, contentLength); } catch (IOException e) { - HttpURLConnection connection = (HttpURLConnection)request.getConnection(); - throw new IOException("Cannot download '" + location + - ", headers: " + connection.getHeaderFields() + - "', response code: " + connection.getResponseCode() + - ", response message: " + connection.getResponseMessage(), e); + throw new IOException(HttpRequests.createErrorMessage(e, request), e); } return null; } }); } catch (IOException e) { - throw new IOException("Cannot download '" + location, e); + throw new IOException("Cannot download " + location, e); } } diff --git a/platform/platform-api/src/com/intellij/util/io/HttpRequests.java b/platform/platform-api/src/com/intellij/util/io/HttpRequests.java index ed9add9363bb..08a5e6063d2d 100644 --- a/platform/platform-api/src/com/intellij/util/io/HttpRequests.java +++ b/platform/platform-api/src/com/intellij/util/io/HttpRequests.java @@ -76,6 +76,17 @@ public abstract class HttpRequests { protected HttpRequests() { } + @NotNull + public static String createErrorMessage(@NotNull IOException e, @NotNull Request request) throws IOException { + URLConnection connection = request.getConnection(); + String errorMessage = "Cannot download '" + connection.getURL().toExternalForm() + "': " + e.getMessage() + "\n, headers: " + connection.getHeaderFields(); + if (connection instanceof HttpURLConnection) { + HttpURLConnection httpConnection = (HttpURLConnection)connection; + errorMessage += "\n, response: " + httpConnection.getResponseCode() + ' ' + httpConnection.getResponseMessage(); + } + return errorMessage; + } + public abstract static class RequestBuilder { private final String myUrl; private int myConnectTimeout = HttpConfigurable.CONNECTION_TIMEOUT; @@ -269,14 +280,7 @@ public abstract class HttpRequests { deleteFile = false; } catch (IOException e) { - URLConnection connection = getConnection(); - String errorMessage = "Cannot download '" + builder.myUrl + ", headers: " + connection.getHeaderFields(); - if (connection instanceof HttpURLConnection) { - HttpURLConnection httpConnection = (HttpURLConnection)connection; - errorMessage += "', response code: " + httpConnection.getResponseCode() - + ", response message: " + httpConnection.getResponseMessage(); - } - throw new IOException(errorMessage, e); + throw new IOException(createErrorMessage(e, this), e); } finally { try {