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 2ff24a33..7c430f2e 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; @@ -56,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; @@ -117,6 +119,7 @@ public static RouterBuilder builder() { HttpClientFactory httpClientFactory, int routeMultiplier, @Nullable OAuthProvider oAuthProvider, + GateleenExceptionFactory exceptionFactory, Handler... doneHandlers) { this.storage = storage; this.properties = properties; @@ -135,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); @@ -324,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); + 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 6db535ed..f7ef9685 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; + } } 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 a53a6a0f..44d239bd 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);