You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by am...@apache.org on 2021/02/22 03:09:31 UTC

[cxf] branch master updated: MP response exception mappers in async methods

This is an automated email from the ASF dual-hosted git repository.

amccright pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new a84f6e2  MP response exception mappers in async methods
a84f6e2 is described below

commit a84f6e203940e93d81406031d571052ccb4e3fef
Author: Andy McCright <j....@gmail.com>
AuthorDate: Sun Feb 21 21:08:32 2021 -0600

    MP response exception mappers in async methods
    
    Signed-off-by: Andy McCright <j....@gmail.com>
---
 .../client/proxy/MicroProfileClientProxyImpl.java  | 14 +++++++++++--
 .../apache/cxf/microprofile/client/AsyncTest.java  | 23 ++++++++++++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/proxy/MicroProfileClientProxyImpl.java b/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/proxy/MicroProfileClientProxyImpl.java
index 48e392d..c309cdd 100644
--- a/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/proxy/MicroProfileClientProxyImpl.java
+++ b/rt/rs/microprofile-client/src/main/java/org/apache/cxf/microprofile/client/proxy/MicroProfileClientProxyImpl.java
@@ -30,6 +30,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.WeakHashMap;
 import java.util.concurrent.Callable;
+import java.util.concurrent.CompletionException;
 import java.util.concurrent.CompletionStage;
 import java.util.concurrent.ExecutorService;
 import java.util.logging.Level;
@@ -194,16 +195,25 @@ public class MicroProfileClientProxyImpl extends ClientProxyImpl {
         for (ResponseExceptionMapper<?> mapper : mappers) {
             if (mapper.handles(r.getStatus(), r.getHeaders())) {
                 Throwable t = mapper.toThrowable(r);
+                if (t == null) {
+                    continue;
+                }
                 if (t instanceof RuntimeException) {
                     throw t;
-                } else if (t != null && m.getExceptionTypes() != null) {
+                } else if (CompletionStage.class.isAssignableFrom(m.getReturnType())) {
+                    throw new CompletionException(t);
+                } else if (m.getExceptionTypes() != null) {
                     // its a checked exception, make sure its declared
                     for (Class<?> c : m.getExceptionTypes()) {
                         if (c.isAssignableFrom(t.getClass())) {
                             throw t;
                         }
                     }
-                    // TODO Log the unhandled declarable
+                    if (LOG.isLoggable(Level.FINEST)) {
+                        LOG.log(Level.FINEST, "ResponseExceptionMapper, " + mapper.getClass().getName() + ", handles " 
+                            + "response, but client method does not declare it's Throwable type, " 
+                            + t.getClass().getName());
+                    }
                 }
             }
         }
diff --git a/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/AsyncTest.java b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/AsyncTest.java
index 1cd9104..fc4905c 100644
--- a/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/AsyncTest.java
+++ b/rt/rs/microprofile-client/src/test/java/org/apache/cxf/microprofile/client/AsyncTest.java
@@ -19,15 +19,18 @@
 package org.apache.cxf.microprofile.client;
 
 import java.net.URI;
+import java.util.concurrent.CompletionStage;
 import java.util.concurrent.TimeUnit;
 
 import okhttp3.mockwebserver.MockResponse;
 import okhttp3.mockwebserver.MockWebServer;
 import org.apache.cxf.microprofile.client.mock.AsyncClient;
+import org.apache.cxf.microprofile.client.mock.NotFoundExceptionMapper;
 import org.eclipse.microprofile.rest.client.RestClientBuilder;
 
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
@@ -53,4 +56,24 @@ public class AsyncTest {
 
         assertTrue("Hello World".equals(combined) || "World Hello".equals(combined));
     }
+
+    @Test
+    public void testAsyncClientCanMapExceptionResponses() throws Exception {
+        MockWebServer mockWebServer = new MockWebServer();
+        URI uri = mockWebServer.url("/").uri();
+
+        AsyncClient client = RestClientBuilder.newBuilder()
+                                              .baseUri(uri)
+                                              .connectTimeout(5, TimeUnit.SECONDS)
+                                              .readTimeout(5, TimeUnit.SECONDS)
+                                              .register(NotFoundExceptionMapper.class)
+                                              .build(AsyncClient.class);
+        mockWebServer.enqueue(new MockResponse().setResponseCode(404));
+
+        CompletionStage cs = client.get().exceptionally(t -> {
+            Throwable t2 = t.getCause();
+            return t.getClass().getSimpleName() + ":" + (t2 == null ? "null" : t2.getClass().getSimpleName());
+        });
+        assertEquals("CompletionException:NoSuchEntityException", cs.toCompletableFuture().get(10, TimeUnit.SECONDS));
+    }
 }