Permalink
Browse files

restclient: add method to get the error message from the server.

  • Loading branch information...
1 parent dd0445a commit a6cc6dd44f39dadd70811f0ed2cd9a5f2827ea56 @kgilmer kgilmer committed Oct 28, 2011
Showing with 25 additions and 9 deletions.
  1. +25 −9 org.touge.restclient/src/org/touge/restclient/ReSTClient.java
@@ -263,7 +263,12 @@ public void handleError(int code, String message) throws IOException {
/**
* @return true if error code or an exception is raised, false otherwise.
*/
- boolean isError();
+ boolean isError();
+
+ /**
+ * @return error message or null if failure to get message from server.
+ */
+ public abstract String getErrorMessage();
}
/**
@@ -666,14 +671,7 @@ public boolean isDone() {
@Override
public T getContent() throws IOException {
if (isError()) {
- String serverMessage = connection.getResponseMessage();
- byte[] errorMessage = readStream(connection.getErrorStream());
- if (errorMessage != null && errorMessage.length > 0) {
- if (connection.getContentEncoding() != null)
- serverMessage = new String(errorMessage, connection.getContentEncoding());
- else
- serverMessage = new String(errorMessage, "UTF-8");
- }
+ String serverMessage = getErrorMessage();
if (responseBuffer != null) {
debugMid(responseBuffer, serverMessage);
@@ -714,6 +712,24 @@ public T getContent() throws IOException {
return response;
}
+
+ @Override
+ public String getErrorMessage() {
+ try {
+ String errorMessage = connection.getResponseMessage();
+ byte[] serverMessage = readStream(connection.getErrorStream());
+ if (serverMessage != null && serverMessage.length > 0) {
+ if (connection.getContentEncoding() != null)
+ errorMessage = new String(serverMessage, connection.getContentEncoding());
+ else
+ errorMessage = new String(serverMessage, "UTF-8");
+ }
+
+ return errorMessage;
+ } catch (IOException e) {
+ return null;
+ }
+ }
};
}

0 comments on commit a6cc6dd

Please sign in to comment.