From 23175fa0d5286e2df582291ab6bc724a9542cfaa Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 20 Jul 2016 14:18:31 +0200 Subject: [PATCH] [platform] fault-tolerant diagnostic composition for HttpRequests (IDEA-156936) --- .../com/intellij/util/io/HttpRequests.java | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) 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 567cf4d68501..acbdc552ab7c 100644 --- a/platform/platform-api/src/com/intellij/util/io/HttpRequests.java +++ b/platform/platform-api/src/com/intellij/util/io/HttpRequests.java @@ -64,6 +64,9 @@ public final class HttpRequests { public interface Request { + @NotNull + String getURL(); + @NotNull URLConnection getConnection() throws IOException; @@ -129,17 +132,23 @@ public final class HttpRequests { } @NotNull - public static String createErrorMessage(@NotNull IOException e, @NotNull Request request, boolean includeHeaders) throws IOException { - URLConnection connection = request.getConnection(); + public static String createErrorMessage(@NotNull IOException e, @NotNull Request request, boolean includeHeaders) { StringBuilder builder = new StringBuilder(); - builder.append("Cannot download '").append(connection.getURL().toExternalForm()).append("': ").append(e.getMessage()); - if (includeHeaders) { - builder.append("\n, headers: ").append(connection.getHeaderFields()); - } - if (connection instanceof HttpURLConnection) { - HttpURLConnection httpConnection = (HttpURLConnection)connection; - builder.append("\n, response: ").append(httpConnection.getResponseCode()).append(' ').append(httpConnection.getResponseMessage()); + + builder.append("Cannot download '").append(request.getURL()).append("': ").append(e.getMessage()); + + try { + URLConnection connection = request.getConnection(); + if (includeHeaders) { + builder.append("\n, headers: ").append(connection.getHeaderFields()); + } + if (connection instanceof HttpURLConnection) { + HttpURLConnection httpConnection = (HttpURLConnection)connection; + builder.append("\n, response: ").append(httpConnection.getResponseCode()).append(' ').append(httpConnection.getResponseMessage()); + } } + catch (Throwable ignored) { } + return builder.toString(); } @@ -249,6 +258,12 @@ public final class HttpRequests { myBuilder = builder; } + @NotNull + @Override + public String getURL() { + return myBuilder.myUrl; + } + @NotNull @Override public URLConnection getConnection() throws IOException {