From 04cffc5df44d7d97557b55869fd61d4f450fe2d9 Mon Sep 17 00:00:00 2001 From: Damiano Renfer Date: Wed, 27 Mar 2024 10:46:16 +0100 Subject: [PATCH] Fix StorkClientRequestFilter exception handling Exceptions were not caught properly because a Uni subscription was done outside a try-catch block. --- .../client/impl/StorkClientRequestFilter.java | 105 +++++++++--------- 1 file changed, 50 insertions(+), 55 deletions(-) diff --git a/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/StorkClientRequestFilter.java b/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/StorkClientRequestFilter.java index 60990009a9d88..92ec1af83341f 100644 --- a/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/StorkClientRequestFilter.java +++ b/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/StorkClientRequestFilter.java @@ -15,9 +15,7 @@ import org.jboss.resteasy.reactive.client.spi.ResteasyReactiveClientRequestFilter; import io.smallrye.mutiny.Multi; -import io.smallrye.mutiny.Uni; import io.smallrye.stork.Stork; -import io.smallrye.stork.api.ServiceInstance; @Priority(Priorities.AUTHENTICATION) @Provider @@ -34,67 +32,64 @@ public void filter(ResteasyReactiveClientRequestContext requestContext) { } requestContext.suspend(); - Uni serviceInstance; boolean measureTime = shouldMeasureTime(requestContext.getResponseType()); try { - serviceInstance = Stork.getInstance() + Stork.getInstance() .getService(serviceName) - .selectInstanceAndRecordStart(measureTime); - } catch (Throwable e) { - log.error("Error selecting service instance for serviceName: " + serviceName, e); - requestContext.resume(e); - return; - } - - serviceInstance.subscribe() - .with(instance -> { - boolean isHttps = instance.isSecure() || "storks".equals(uri.getScheme()); - String scheme = isHttps ? "https" : "http"; - try { - // In the case the service instance does not set the host and/or port - String host = instance.getHost() == null ? "localhost" : instance.getHost(); - int port = instance.getPort(); - if (instance.getPort() == 0) { - if (isHttps) { - port = 433; - } else { - port = 80; - } - } - // Service instance can also contain an optional path. - Optional path = instance.getPath(); - String actualPath = uri.getRawPath(); - if (path.isPresent()) { - var p = path.get(); - if (!p.startsWith("/")) { - p = "/" + p; + .selectInstanceAndRecordStart(measureTime) + .subscribe() + .with(instance -> { + boolean isHttps = instance.isSecure() || "storks".equals(uri.getScheme()); + String scheme = isHttps ? "https" : "http"; + try { + // In the case the service instance does not set the host and/or port + String host = instance.getHost() == null ? "localhost" : instance.getHost(); + int port = instance.getPort(); + if (instance.getPort() == 0) { + if (isHttps) { + port = 433; + } else { + port = 80; + } } - if (actualPath == null) { - actualPath = p; - } else { - // Append both. - if (actualPath.startsWith("/") || p.endsWith("/")) { - actualPath = p + actualPath; + // Service instance can also contain an optional path. + Optional path = instance.getPath(); + String actualPath = uri.getRawPath(); + if (path.isPresent()) { + var p = path.get(); + if (!p.startsWith("/")) { + p = "/" + p; + } + if (actualPath == null) { + actualPath = p; } else { - actualPath = p + "/" + actualPath; + // Append both. + if (actualPath.startsWith("/") || p.endsWith("/")) { + actualPath = p + actualPath; + } else { + actualPath = p + "/" + actualPath; + } } } + //To avoid the path double encoding we create uri with path=null and set the path after + URI newUri = new URI(scheme, + uri.getUserInfo(), host, port, + null, uri.getQuery(), uri.getFragment()); + URI build = UriBuilder.fromUri(newUri).path(actualPath).build(); + requestContext.setUri(build); + if (measureTime && instance.gatherStatistics()) { + requestContext.setCallStatsCollector(instance); + } + requestContext.resume(); + } catch (URISyntaxException e) { + requestContext.resume(new IllegalArgumentException("Invalid URI", e)); } - //To avoid the path double encoding we create uri with path=null and set the path after - URI newUri = new URI(scheme, - uri.getUserInfo(), host, port, - null, uri.getQuery(), uri.getFragment()); - URI build = UriBuilder.fromUri(newUri).path(actualPath).build(); - requestContext.setUri(build); - if (measureTime && instance.gatherStatistics()) { - requestContext.setCallStatsCollector(instance); - } - requestContext.resume(); - } catch (URISyntaxException e) { - requestContext.resume(new IllegalArgumentException("Invalid URI", e)); - } - }, - requestContext::resume); + }, + requestContext::resume); + } catch (Throwable e) { + log.error("Error selecting service instance for serviceName: " + serviceName, e); + requestContext.resume(e); + } } }