[platform] fault-tolerant diagnostic composition for HttpRequests (IDEA-156936)

This commit is contained in:
Roman Shevchenko
2016-07-20 14:18:54 +02:00
parent e2d97aefff
commit 23175fa0d5
@@ -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 {