[PATCH] Slightly improved error handling if response entity could not be parsed

Wald Commits scm-commit at wald.intevation.org
Wed Jun 13 15:37:16 CEST 2018


# HG changeset patch
# User gernotbelger
# Date 1528897032 -7200
# Node ID c92b50c9c99a2e8ec2d75de031de6d54c152acf1
# Parent  6e694ec612469f9aa3811d328b9652eac6b83e15
Slightly improved error handling if response entity could not be parsed.

diff -r 6e694ec61246 -r c92b50c9c99a src/main/java/org/dive4elements/artifacts/httpclient/http/HttpClientImpl.java
--- a/src/main/java/org/dive4elements/artifacts/httpclient/http/HttpClientImpl.java	Mon Dec 28 17:54:40 2015 +0100
+++ b/src/main/java/org/dive4elements/artifacts/httpclient/http/HttpClientImpl.java	Wed Jun 13 15:37:12 2018 +0200
@@ -146,15 +146,23 @@
 
         try {
             String   url    = serverUrl + "/create";
-            Document result = (Document) handler.handle(doPost(url, doc));
+            Response response = doPost(url, doc);
+            try {
+                Document result = (Document) handler.handle(response);
 
-            return creator == null
-                ? ArtifactProtocolUtils.extractArtifact(result)
-                : creator.create(result);
+                return creator == null
+                    ? ArtifactProtocolUtils.extractArtifact(result)
+                    : creator.create(result);
+            }
+            catch (IOException e) {
+                String entityAsText = response.getEntityAsText();
+                throw new ConnectionException(
+                        "Connection to server failed. No Artifact created. Response was: " + entityAsText, e);
+            }
         }
         catch (IOException ioe) {
             throw new ConnectionException(
-                "Connection to server failed. No Artifact created.");
+                "Connection to server failed. No Artifact created.", ioe);
         }
     }
 
diff -r 6e694ec61246 -r c92b50c9c99a src/main/java/org/dive4elements/artifacts/httpclient/http/response/DocumentResponseHandler.java
--- a/src/main/java/org/dive4elements/artifacts/httpclient/http/response/DocumentResponseHandler.java	Mon Dec 28 17:54:40 2015 +0100
+++ b/src/main/java/org/dive4elements/artifacts/httpclient/http/response/DocumentResponseHandler.java	Wed Jun 13 15:37:12 2018 +0200
@@ -9,24 +9,19 @@
 
 import java.io.IOException;
 
+import org.dive4elements.artifacts.httpclient.utils.XMLUtils;
 import org.restlet.Response;
 import org.restlet.representation.Representation;
 
-import org.dive4elements.artifacts.httpclient.utils.XMLUtils;
-
-
 /**
  * @author <a href="mailto:ingo.weinzierl at intevation.de">Ingo Weinzierl</a>
  */
 public class DocumentResponseHandler implements ResponseHandler {
 
-    public DocumentResponseHandler() {
+    @Override
+    public Object handle(final Response response) throws IOException {
+        final Representation output = response.getEntity();
+            // FIXME: stream not closed?
+            return XMLUtils.readDocument(output.getStream());
     }
-
-    @Override
-    public Object handle(Response response) throws IOException {
-        Representation output = response.getEntity();
-        return XMLUtils.readDocument(output.getStream());
-    }
-}
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:
+}
\ No newline at end of file


More information about the Dive4Elements-commits mailing list