From 8036bf08f04c56dab04abe469acda95e2698cf60 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Sun, 7 Nov 2010 01:45:40 +0100 Subject: [PATCH] improved error response code to include url encoded params which makes ec2 a lot easier to debug --- .../jclouds/http/HttpResponseException.java | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/org/jclouds/http/HttpResponseException.java b/core/src/main/java/org/jclouds/http/HttpResponseException.java index f7e035657a..d938a8e2a9 100644 --- a/core/src/main/java/org/jclouds/http/HttpResponseException.java +++ b/core/src/main/java/org/jclouds/http/HttpResponseException.java @@ -19,8 +19,13 @@ package org.jclouds.http; +import java.io.IOException; + import javax.annotation.Nullable; +import org.jclouds.io.payloads.StringPayload; +import org.jclouds.util.Utils; + /** * Represents an error obtained from an HttpResponse. * @@ -34,15 +39,14 @@ public class HttpResponseException extends RuntimeException { protected final HttpResponse response; private String content; - public HttpResponseException(String message, HttpCommand command, - @Nullable HttpResponse response, Throwable cause) { + public HttpResponseException(String message, HttpCommand command, @Nullable HttpResponse response, Throwable cause) { super(message, cause); this.command = command; this.response = response; } - public HttpResponseException(String message, HttpCommand command, - @Nullable HttpResponse response, String content, Throwable cause) { + public HttpResponseException(String message, HttpCommand command, @Nullable HttpResponse response, String content, + Throwable cause) { super(message, cause); this.command = command; this.response = response; @@ -50,15 +54,13 @@ public class HttpResponseException extends RuntimeException { } public HttpResponseException(HttpCommand command, HttpResponse response, Throwable cause) { - this(String.format("command: %1$s failed with response: %2$s", command.getRequest() - .getRequestLine(), response.getStatusLine()), command, response, cause); + this(String.format("command: %1$s failed with response: %2$s", command.getRequest().getRequestLine(), + response.getStatusLine()), command, response, cause); } - public HttpResponseException(HttpCommand command, HttpResponse response, String content, - Throwable cause) { - this(String.format("command: %1$s failed with response: %2$s; content: [%3$s]", command - .getRequest().getRequestLine(), response.getStatusLine()), command, response, - content, cause); + public HttpResponseException(HttpCommand command, HttpResponse response, String content, Throwable cause) { + this(String.format("command: %1$s failed with response: %2$s; content: [%3$s]", command.getRequest() + .getRequestLine(), response.getStatusLine()), command, response, content, cause); } public HttpResponseException(String message, HttpCommand command, @Nullable HttpResponse response) { @@ -67,8 +69,7 @@ public class HttpResponseException extends RuntimeException { this.response = response; } - public HttpResponseException(String message, HttpCommand command, - @Nullable HttpResponse response, String content) { + public HttpResponseException(String message, HttpCommand command, @Nullable HttpResponse response, String content) { super(message); this.command = command; this.response = response; @@ -76,14 +77,29 @@ public class HttpResponseException extends RuntimeException { } public HttpResponseException(HttpCommand command, HttpResponse response) { - this(String.format("command: %1$s failed with response: %2$s", command.getRequest() - .getRequestLine(), response.getStatusLine()), command, response); + this(String.format("request: %s %sfailed with response: %s", command.getRequest().getRequestLine(), + requestPayloadIfStringOrFormIfNotReturnEmptyString(command.getRequest()), response.getStatusLine()), + command, response); + } + + static String requestPayloadIfStringOrFormIfNotReturnEmptyString(HttpRequest request) { + if (request.getPayload() != null + && ("application/x-www-form-urlencoded".equals(request.getPayload().getContentMetadata().getContentType()) || request + .getPayload() instanceof StringPayload) + && request.getPayload().getContentMetadata().getContentLength() != null + && request.getPayload().getContentMetadata().getContentLength() < 1024) { + try { + return String.format(" [%s] ", request.getPayload() instanceof StringPayload ? request.getPayload() + .getRawContent() : Utils.toStringAndClose(request.getPayload().getInput())); + } catch (IOException e) { + } + } + return ""; } public HttpResponseException(HttpCommand command, HttpResponse response, String content) { - this(String.format("command: %1$s failed with response: %2$s; content: [%3$s]", command - .getRequest().getRequestLine(), response.getStatusLine(), content), command, - response, content); + this(String.format("command: %s failed with response: %s; content: [%s]", command.getRequest().getRequestLine(), + response.getStatusLine(), content), command, response, content); } public HttpCommand getCommand() {