From 178488e67819a4b9d57c9fca60aaf7795303c5f8 Mon Sep 17 00:00:00 2001 From: Xin Zheng Date: Tue, 18 Jun 2024 15:05:16 +0700 Subject: [PATCH 1/2] try to get more info when error happens --- .../main/java/org/swisspush/gateleen/routing/Router.java | 5 ++++- .../org/swisspush/gateleen/routing/StorageForwarder.java | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/Router.java b/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/Router.java index 2ff24a333..2c768eca1 100755 --- a/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/Router.java +++ b/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/Router.java @@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory; import org.swisspush.gateleen.core.configuration.ConfigurationResourceManager; import org.swisspush.gateleen.core.configuration.ConfigurationResourceObserver; +import org.swisspush.gateleen.core.exception.GateleenExceptionFactory; import org.swisspush.gateleen.core.http.HttpClientFactory; import org.swisspush.gateleen.core.http.RequestLoggerFactory; import org.swisspush.gateleen.core.logging.LoggableResource; @@ -85,6 +86,8 @@ public class Router implements Refreshable, LoggableResource, ConfigurationResou private OAuthStrategy oAuthStrategy = null; private BasicAuthStrategy basicAuthStrategy; + private static GateleenExceptionFactory gateleenExceptionFactory = GateleenExceptionFactory.newGateleenThriftyExceptionFactory(); + /** * The multiplier applied to routes, typically the number of {@link Router} instances in a cluster. */ @@ -324,7 +327,7 @@ private void createForwarders(List rules, io.vertx.ext.web.Router newRoute vertx.eventBus()); } else if (rule.getStorage() != null) { forwarder = new StorageForwarder(vertx.eventBus(), rule, loggingResourceManager, logAppenderRepository, - monitoringHandler); + monitoringHandler, gateleenExceptionFactory); } else if (rule.getScheme().equals("local")) { forwarder = new Forwarder(vertx, selfClient, rule, this.storage, loggingResourceManager, logAppenderRepository, monitoringHandler, userProfileUri, authStrategy); diff --git a/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/StorageForwarder.java b/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/StorageForwarder.java index a53a6a0f9..44d239bd9 100755 --- a/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/StorageForwarder.java +++ b/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/StorageForwarder.java @@ -15,6 +15,7 @@ import io.vertx.ext.web.RoutingContext; import org.slf4j.Logger; import org.swisspush.gateleen.core.cors.CORSHandler; +import org.swisspush.gateleen.core.exception.GateleenExceptionFactory; import org.swisspush.gateleen.core.http.HeaderFunctions; import org.swisspush.gateleen.core.http.HttpRequest; import org.swisspush.gateleen.core.http.RequestLoggerFactory; @@ -40,13 +41,17 @@ public class StorageForwarder extends AbstractForwarder { private Pattern urlPattern; private String address; private CORSHandler corsHandler; + private GateleenExceptionFactory gateleenExceptionFactory; - public StorageForwarder(EventBus eventBus, Rule rule, LoggingResourceManager loggingResourceManager, LogAppenderRepository logAppenderRepository, MonitoringHandler monitoringHandler) { + public StorageForwarder(EventBus eventBus, Rule rule, LoggingResourceManager loggingResourceManager, + LogAppenderRepository logAppenderRepository, MonitoringHandler monitoringHandler, + GateleenExceptionFactory gateleenExceptionFactory) { super(rule, loggingResourceManager, logAppenderRepository, monitoringHandler); this.eventBus = eventBus; this.address = Address.storageAddress() + "-" + rule.getStorage(); urlPattern = Pattern.compile(rule.getUrlPattern()); corsHandler = new CORSHandler(); + this.gateleenExceptionFactory = gateleenExceptionFactory; } @Override @@ -97,7 +102,7 @@ public void handle(final RoutingContext ctx) { response.setStatusCode(StatusCode.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatusMessage(statusMessage); response.end(); - log.error("Storage request failed", result.cause()); + log.error(statusMessage, gateleenExceptionFactory.newException(result.cause())); } else { Buffer buffer = result.result().body(); int headerLength = buffer.getInt(0); From 5547874473125e7fd1dd65de31bf8470141c5e57 Mon Sep 17 00:00:00 2001 From: Xin Zheng Date: Thu, 20 Jun 2024 15:27:22 +0700 Subject: [PATCH 2/2] exceptionFactory can be pass in --- .../java/org/swisspush/gateleen/routing/Router.java | 7 ++++--- .../swisspush/gateleen/routing/RouterBuilder.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/Router.java b/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/Router.java index 2c768eca1..7c430f2e5 100755 --- a/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/Router.java +++ b/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/Router.java @@ -57,6 +57,7 @@ public class Router implements Refreshable, LoggableResource, ConfigurationResou private final String rulesUri; private final String userProfileUri; private final String serverUri; + private final GateleenExceptionFactory exceptionFactory; private io.vertx.ext.web.Router router; private final LoggingResourceManager loggingResourceManager; private final LogAppenderRepository logAppenderRepository; @@ -86,8 +87,6 @@ public class Router implements Refreshable, LoggableResource, ConfigurationResou private OAuthStrategy oAuthStrategy = null; private BasicAuthStrategy basicAuthStrategy; - private static GateleenExceptionFactory gateleenExceptionFactory = GateleenExceptionFactory.newGateleenThriftyExceptionFactory(); - /** * The multiplier applied to routes, typically the number of {@link Router} instances in a cluster. */ @@ -120,6 +119,7 @@ public static RouterBuilder builder() { HttpClientFactory httpClientFactory, int routeMultiplier, @Nullable OAuthProvider oAuthProvider, + GateleenExceptionFactory exceptionFactory, Handler... doneHandlers) { this.storage = storage; this.properties = properties; @@ -138,6 +138,7 @@ public static RouterBuilder builder() { this.doneHandlers = doneHandlers; this.routeMultiplier = routeMultiplier; this.oAuthProvider = oAuthProvider; + this.exceptionFactory = exceptionFactory; if (oAuthProvider != null) { this.oAuthStrategy = new OAuthStrategy(oAuthProvider); @@ -327,7 +328,7 @@ private void createForwarders(List rules, io.vertx.ext.web.Router newRoute vertx.eventBus()); } else if (rule.getStorage() != null) { forwarder = new StorageForwarder(vertx.eventBus(), rule, loggingResourceManager, logAppenderRepository, - monitoringHandler, gateleenExceptionFactory); + monitoringHandler, exceptionFactory); } else if (rule.getScheme().equals("local")) { forwarder = new Forwarder(vertx, selfClient, rule, this.storage, loggingResourceManager, logAppenderRepository, monitoringHandler, userProfileUri, authStrategy); diff --git a/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/RouterBuilder.java b/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/RouterBuilder.java index 6db535ed9..f7ef9685f 100644 --- a/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/RouterBuilder.java +++ b/gateleen-routing/src/main/java/org/swisspush/gateleen/routing/RouterBuilder.java @@ -7,6 +7,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.swisspush.gateleen.core.configuration.ConfigurationResourceManager; +import org.swisspush.gateleen.core.exception.GateleenExceptionFactory; import org.swisspush.gateleen.core.http.HttpClientFactory; import org.swisspush.gateleen.core.storage.ResourceStorage; import org.swisspush.gateleen.logging.LogAppenderRepository; @@ -52,6 +53,7 @@ public class RouterBuilder { private int routeMultiplier = Router.DEFAULT_ROUTER_MULTIPLIER; private OAuthProvider oAuthProvider; + private GateleenExceptionFactory exceptionFactory; RouterBuilder() { // PackagePrivate, as clients should use "Router.builder()" and not this class here directly. @@ -67,6 +69,10 @@ public Router build() { defaultRouteTypes = all(); } + if (this.exceptionFactory == null) { + this.exceptionFactory = GateleenExceptionFactory.newGateleenThriftyExceptionFactory(); + } + Handler[] doneHandlersArray; if (doneHandlers == null || doneHandlers.isEmpty()) { logger.debug("No doneHandlers specified."); @@ -102,6 +108,7 @@ public Router build() { httpClientFactory, routeMultiplier, oAuthProvider, + exceptionFactory, doneHandlersArray ); if (resourceLoggingEnabled) { @@ -273,4 +280,9 @@ public RouterBuilder withRouteMultiplier(int routeMultiplier) { this.routeMultiplier = routeMultiplier; return this; } + + public RouterBuilder withExceptionFactory(GateleenExceptionFactory exceptionFactory) { + this.exceptionFactory = exceptionFactory; + return this; + } }