From 6ab694ee3148543286a10adbcae5e415906f8951 Mon Sep 17 00:00:00 2001 From: timp21337 Date: Wed, 16 Oct 2013 11:54:46 +0100 Subject: [PATCH 1/2] Add HttpIOExcption to enable the extraction of responseCode --- .../java/us/monoid/web/AbstractResource.java | 4 +- .../java/us/monoid/web/HttpIOException.java | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/main/java/us/monoid/web/HttpIOException.java diff --git a/src/main/java/us/monoid/web/AbstractResource.java b/src/main/java/us/monoid/web/AbstractResource.java index d710fe8..8672fe7 100644 --- a/src/main/java/us/monoid/web/AbstractResource.java +++ b/src/main/java/us/monoid/web/AbstractResource.java @@ -56,8 +56,8 @@ void fill(URLConnection anUrlConnection) throws IOException { // close the errorstream es.close(); - throw new IOException("Error while reading from " + conn.getRequestMethod() + ": [" + conn.getResponseCode() + "] " - + conn.getResponseMessage() + "\n" + new String(baos.toByteArray(), "UTF-8"), e); + throw new HttpIOException(conn.getRequestMethod(), conn.getResponseCode(), conn.getResponseMessage(), + new String(baos.toByteArray(), "UTF-8"), e); } else { throw e; } diff --git a/src/main/java/us/monoid/web/HttpIOException.java b/src/main/java/us/monoid/web/HttpIOException.java new file mode 100644 index 0000000..ec7eb29 --- /dev/null +++ b/src/main/java/us/monoid/web/HttpIOException.java @@ -0,0 +1,47 @@ +package us.monoid.web; + +import java.io.IOException; + +/** + * @author timp21337 + * @since 2103-10-15 + */ +public class HttpIOException extends IOException { + + private String requestMethod; + private int responseCode; + private String responseMessage; + private String content; + + public HttpIOException(String requestMethod, int responseCode, + String responseMessage, String content, IOException e) { + super(e); + this.requestMethod = requestMethod; + this.responseCode = responseCode; + this.responseMessage = responseMessage; + this.content = content; + } + + public String getRequestMethod() { + return requestMethod; + } + public int getResponseCode() { + return responseCode; + } + public String getResponseMessage() { + return responseMessage; + } + public String getContent() { + return content; + } + + @Override + public String getMessage() { + return "Error while reading from " + getRequestMethod() + + ": [" + getResponseCode() + "] " + + getResponseMessage() + "\n" + + getContent(); + } + + +} From 86d98957a230e4733340d974554f6218dc0620cf Mon Sep 17 00:00:00 2001 From: timp21337 Date: Thu, 17 Oct 2013 11:12:50 +0100 Subject: [PATCH 2/2] Add toString, copying Gary Leeson --- src/main/java/us/monoid/web/HttpIOException.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/us/monoid/web/HttpIOException.java b/src/main/java/us/monoid/web/HttpIOException.java index ec7eb29..7af8f9f 100644 --- a/src/main/java/us/monoid/web/HttpIOException.java +++ b/src/main/java/us/monoid/web/HttpIOException.java @@ -43,5 +43,13 @@ public String getMessage() { + getContent(); } - + @Override + public String toString() { + return "HttpIOException{" + + "requestMethod='" + requestMethod + '\'' + + ", responseCode=" + responseCode + + ", responseMessage='" + responseMessage + '\'' + + ", content='" + content + '\'' + + '}'; + } }