diff --git a/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsConfig.java b/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsConfig.java index 0644872..479e337 100644 --- a/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsConfig.java +++ b/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsConfig.java @@ -52,6 +52,7 @@ public class GoogleAnalyticsConfig { private boolean discoverRequestParameters = true; private boolean gatherStats = false; private RequestParameterDiscoverer requestParameterDiscoverer = new DefaultRequestParameterDiscoverer(); + private GoogleAnalyticsExceptionHandler exceptionHandler; public RequestParameterDiscoverer getRequestParameterDiscoverer() { return requestParameterDiscoverer; @@ -125,7 +126,7 @@ public GoogleAnalyticsConfig setDeriveSystemParameters(boolean deriveSystemPrope *

*/ public GoogleAnalyticsConfig setDiscoverRequestParameters(boolean discoverSystemParameters) { - this.discoverRequestParameters = discoverSystemParameters; + discoverRequestParameters = discoverSystemParameters; return this; } @@ -404,4 +405,17 @@ public GoogleAnalyticsConfig setBatchSize(int batchSize) { return this; } + public GoogleAnalyticsExceptionHandler getExceptionHandler() { + return exceptionHandler; + } + + /** + * Set an exception handler which will implement the behavior in case of any exceptions. If not set, default + * behavior is to log a warning message. + */ + public GoogleAnalyticsConfig setExceptionHandler(GoogleAnalyticsExceptionHandler exceptionHandler) { + this.exceptionHandler = exceptionHandler; + return this; + } + } diff --git a/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsExceptionHandler.java b/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsExceptionHandler.java new file mode 100644 index 0000000..5b9ecdf --- /dev/null +++ b/src/main/java/com/brsanthu/googleanalytics/GoogleAnalyticsExceptionHandler.java @@ -0,0 +1,22 @@ +package com.brsanthu.googleanalytics; + +/** + * Models a handler which can be used to handle exceptions thrown while sending GA requests to google. This can be set + * as config parameter as below. + * + *
+ *  GoogleAnalytics propagatingGa = GoogleAnalytics.builder()withConfig(
+ *      new GoogleAnalyticsConfig().setExceptionHandler(new YourExceptionHandler())
+ *  ).build();
+ * 
+ * + *

+ * Library comes with following implementations. + *

+ */ +@FunctionalInterface +public interface GoogleAnalyticsExceptionHandler { + void handle(Throwable t); +} diff --git a/src/main/java/com/brsanthu/googleanalytics/PropagatingExceptionHandler.java b/src/main/java/com/brsanthu/googleanalytics/PropagatingExceptionHandler.java new file mode 100644 index 0000000..50255c7 --- /dev/null +++ b/src/main/java/com/brsanthu/googleanalytics/PropagatingExceptionHandler.java @@ -0,0 +1,16 @@ +package com.brsanthu.googleanalytics; + +/** + * Exception handler which propagates the exceptions instead of just logging. + */ +public class PropagatingExceptionHandler implements GoogleAnalyticsExceptionHandler { + + @Override + public void handle(Throwable t) { + if (t instanceof RuntimeException) { + throw (RuntimeException) t; + } + + throw new GoogleAnalyticsException(t); + } +} diff --git a/src/main/java/com/brsanthu/googleanalytics/internal/GoogleAnalyticsImpl.java b/src/main/java/com/brsanthu/googleanalytics/internal/GoogleAnalyticsImpl.java index bd3b0f8..7672fc0 100644 --- a/src/main/java/com/brsanthu/googleanalytics/internal/GoogleAnalyticsImpl.java +++ b/src/main/java/com/brsanthu/googleanalytics/internal/GoogleAnalyticsImpl.java @@ -105,8 +105,12 @@ public GoogleAnalyticsResponse post(GoogleAnalyticsRequest gaReq) { response = postSingle(gaReq); } - } catch (Exception e) { - logger.warn("Exception while sending the Google Analytics tracker request " + gaReq, e); + } catch (Throwable e) { + if (config.getExceptionHandler() != null) { + config.getExceptionHandler().handle(e); + } else { + logger.warn("Exception while sending the Google Analytics tracker request " + gaReq, e); + } } return response; @@ -217,7 +221,7 @@ protected void processParameters(GoogleAnalyticsRequest request, HttpRequest * @param postParms */ protected void processCustomDimensionParameters(GoogleAnalyticsRequest request, HttpRequest req) { - Map customDimParms = new HashMap(); + Map customDimParms = new HashMap<>(); for (String defaultCustomDimKey : defaultRequest.customDimensions().keySet()) { customDimParms.put(defaultCustomDimKey, defaultRequest.customDimensions().get(defaultCustomDimKey)); } @@ -239,7 +243,7 @@ protected void processCustomDimensionParameters(GoogleAnalyticsRequest reques * @param postParms */ protected void processCustomMetricParameters(GoogleAnalyticsRequest request, HttpRequest req) { - Map customMetricParms = new HashMap(); + Map customMetricParms = new HashMap<>(); for (String defaultCustomMetricKey : defaultRequest.custommMetrics().keySet()) { customMetricParms.put(defaultCustomMetricKey, defaultRequest.custommMetrics().get(defaultCustomMetricKey)); } diff --git a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsTest.java b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsTest.java index c60d313..77d1ab1 100644 --- a/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsTest.java +++ b/src/test/java/com/brsanthu/googleanalytics/GoogleAnalyticsTest.java @@ -132,4 +132,22 @@ public void testUserDetails() throws Exception { assertEquals("12345", response.getRequestParams().get("cid")); assertEquals("user2", response.getRequestParams().get("uid")); } + + @Test + void testExceptionHandler() throws Exception { + HttpClient client = mock(HttpClient.class); + when(client.post(ArgumentMatchers.any())).thenThrow(new RuntimeException("Testing Exception")); + + GoogleAnalytics ga = GoogleAnalytics.builder().withHttpClient(client).withConfig(new GoogleAnalyticsConfig()).build(); + + // Since default behavior is to log exception, this function call should work fine. + ga.screenView().send(); + + GoogleAnalytics propagatingGa = GoogleAnalytics.builder().withHttpClient(client).withConfig( + new GoogleAnalyticsConfig().setExceptionHandler(new PropagatingExceptionHandler())).build(); + + assertThatThrownBy(() -> propagatingGa.screenView().send()).hasMessage("Testing Exception"); + + assertThatThrownBy(() -> propagatingGa.screenView().sendAsync().get()).hasMessageContaining("Testing Exception"); + } }