http-client-branch: synchronous send should be on the exception stack http-client-branch
authorchegar
Thu, 07 Jun 2018 14:21:50 +0100
branchhttp-client-branch
changeset 56695 d219df0c7d24
parent 56688 0b633bdb7010
child 56728 bd76f0daf3c2
http-client-branch: synchronous send should be on the exception stack
src/java.net.http/share/classes/jdk/internal/net/http/HttpClientImpl.java
test/jdk/java/net/httpclient/ShortResponseBody.java
test/jdk/java/net/httpclient/ThrowingPublishers.java
test/jdk/java/net/httpclient/ThrowingSubscribers.java
--- a/src/java.net.http/share/classes/jdk/internal/net/http/HttpClientImpl.java	Thu Jun 07 10:45:30 2018 +0100
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/HttpClientImpl.java	Thu Jun 07 14:21:50 2018 +0100
@@ -34,6 +34,7 @@
 import java.net.Authenticator;
 import java.net.CookieHandler;
 import java.net.ProxySelector;
+import java.net.http.HttpTimeoutException;
 import java.nio.ByteBuffer;
 import java.nio.channels.CancelledKeyException;
 import java.nio.channels.ClosedChannelException;
@@ -518,18 +519,29 @@
     send(HttpRequest req, BodyHandler<T> responseHandler)
         throws IOException, InterruptedException
     {
+        CompletableFuture<HttpResponse<T>> cf = null;
         try {
-            return sendAsync(req, responseHandler, null, null).get();
+            cf = sendAsync(req, responseHandler, null, null);
+            return cf.get();
+        } catch (InterruptedException ie) {
+            if (cf != null )
+                cf.cancel(true);
+            throw ie;
         } catch (ExecutionException e) {
-            Throwable t = e.getCause();
-            if (t instanceof Error)
-                throw (Error)t;
-            if (t instanceof RuntimeException)
-                throw (RuntimeException)t;
-            else if (t instanceof IOException)
-                throw Utils.getIOException(t);
+            Throwable throwable = e.getCause();
+
+            if (throwable instanceof IllegalArgumentException)
+                throw new IllegalArgumentException(throwable);
+            else if (throwable instanceof SecurityException)
+                throw new SecurityException(throwable);
+            else if (throwable instanceof HttpTimeoutException)
+                throw new HttpTimeoutException(throwable.getMessage());
+            else if (throwable instanceof IOException)
+                throw new IOException(throwable);
+            //else if (throwable instanceof UncheckedIOException)
+            //    throw new UncheckedIOException(((UncheckedIOException)throwable).getCause());
             else
-                throw new InternalError("Unexpected exception", t);
+                throw new IOException(throwable);
         }
     }
 
--- a/test/jdk/java/net/httpclient/ShortResponseBody.java	Thu Jun 07 10:45:30 2018 +0100
+++ b/test/jdk/java/net/httpclient/ShortResponseBody.java	Thu Jun 07 14:21:50 2018 +0100
@@ -166,7 +166,7 @@
                 String msg = ioe.getMessage();
                 assertTrue(msg.contains(expectedMsg), "exception msg:[" + msg + "]");
                 // synchronous API must have the send method on the stack
-                //TODO: uncomment assertSendMethodOnStack(ioe);
+                assertSendMethodOnStack(ioe);
                 assertNoConnectionExpiredException(ioe);
             }
         }
@@ -232,7 +232,7 @@
                 // "incomplete" since the chunked request body is not completely sent
                 assertTrue(msg.contains("incomplete"), "exception msg:[" + msg + "]");
                 // synchronous API must have the send method on the stack
-                //TODO: uncomment assertSendMethodOnStack(ioe);
+                assertSendMethodOnStack(ioe);
                 assertNoConnectionExpiredException(ioe);
             }
         }
--- a/test/jdk/java/net/httpclient/ThrowingPublishers.java	Thu Jun 07 10:45:30 2018 +0100
+++ b/test/jdk/java/net/httpclient/ThrowingPublishers.java	Thu Jun 07 14:21:50 2018 +0100
@@ -345,8 +345,12 @@
                 try {
                     response = client.send(req, handler);
                 } catch (Error | Exception t) {
-                    if (thrower.test(where, t)) {
-                        System.out.println(now() + "Got expected exception: " + t);
+                    // synchronous send will rethrow exceptions
+                    Throwable throwable = t.getCause();
+                    assert throwable != null;
+
+                    if (thrower.test(where, throwable)) {
+                        System.out.println(now() + "Got expected exception: " + throwable);
                     } else throw causeNotFound(where, t);
                 }
             }
--- a/test/jdk/java/net/httpclient/ThrowingSubscribers.java	Thu Jun 07 10:45:30 2018 +0100
+++ b/test/jdk/java/net/httpclient/ThrowingSubscribers.java	Thu Jun 07 14:21:50 2018 +0100
@@ -409,8 +409,12 @@
                 try {
                     response = client.send(req, handler);
                 } catch (Error | Exception t) {
-                    if (thrower.test(t)) {
-                        System.out.println(now() + "Got expected exception: " + t);
+                    // synchronous send will rethrow exceptions
+                    Throwable throwable = t.getCause();
+                    assert throwable != null;
+
+                    if (thrower.test(throwable)) {
+                        System.out.println(now() + "Got expected exception: " + throwable);
                     } else throw causeNotFound(where, t);
                 }
             }