Skip to content

Commit

Permalink
Add exception handler api
Browse files Browse the repository at this point in the history
  • Loading branch information
brsanthu committed May 19, 2019
1 parent cd052d2 commit 541d9e7
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,7 +126,7 @@ public GoogleAnalyticsConfig setDeriveSystemParameters(boolean deriveSystemPrope
* </p>
*/
public GoogleAnalyticsConfig setDiscoverRequestParameters(boolean discoverSystemParameters) {
this.discoverRequestParameters = discoverSystemParameters;
discoverRequestParameters = discoverSystemParameters;
return this;
}

Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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.
*
* <pre>
* GoogleAnalytics propagatingGa = GoogleAnalytics.builder()withConfig(
* new GoogleAnalyticsConfig().setExceptionHandler(new YourExceptionHandler())
* ).build();
* </pre>
*
* <p>
* Library comes with following implementations.
* <ul>
* <li>{@link PropagatingExceptionHandler} which propagates the exceptions.
* </ul>
*/
@FunctionalInterface
public interface GoogleAnalyticsExceptionHandler {
void handle(Throwable t);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -217,7 +221,7 @@ protected void processParameters(GoogleAnalyticsRequest<?> request, HttpRequest
* @param postParms
*/
protected void processCustomDimensionParameters(GoogleAnalyticsRequest<?> request, HttpRequest req) {
Map<String, String> customDimParms = new HashMap<String, String>();
Map<String, String> customDimParms = new HashMap<>();
for (String defaultCustomDimKey : defaultRequest.customDimensions().keySet()) {
customDimParms.put(defaultCustomDimKey, defaultRequest.customDimensions().get(defaultCustomDimKey));
}
Expand All @@ -239,7 +243,7 @@ protected void processCustomDimensionParameters(GoogleAnalyticsRequest<?> reques
* @param postParms
*/
protected void processCustomMetricParameters(GoogleAnalyticsRequest<?> request, HttpRequest req) {
Map<String, String> customMetricParms = new HashMap<String, String>();
Map<String, String> customMetricParms = new HashMap<>();
for (String defaultCustomMetricKey : defaultRequest.custommMetrics().keySet()) {
customMetricParms.put(defaultCustomMetricKey, defaultRequest.custommMetrics().get(defaultCustomMetricKey));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 541d9e7

Please sign in to comment.