1919import java .util .Objects ;
2020import java .util .UUID ;
2121import java .util .concurrent .ConcurrentHashMap ;
22+ import java .util .function .BiFunction ;
2223
2324import static net .hollowcube .posthog .FeatureFlagEvaluator .evaluateFeatureFlag ;
2425import static net .hollowcube .posthog .FeatureFlagState .REMOTE_EVAL_NOT_ALLOWED ;
@@ -50,6 +51,8 @@ public final class PostHogClientImpl implements PostHogClient {
5051 private final boolean sendFeatureFlagEvents ;
5152 private final Duration featureFlagsRequestTimeout ;
5253
54+ private final BiFunction <Throwable , JsonObject , Boolean > exceptionMiddleware ;
55+
5356 PostHogClientImpl (
5457 @ NotNull Gson gson ,
5558
@@ -65,7 +68,8 @@ public final class PostHogClientImpl implements PostHogClient {
6568 boolean allowRemoteFeatureFlagEvaluation ,
6669 boolean sendFeatureFlagEvents ,
6770 @ NotNull Duration featureFlagsPollingInterval ,
68- @ NotNull Duration featureFlagsRequestTimeout
71+ @ NotNull Duration featureFlagsRequestTimeout ,
72+ @ Nullable BiFunction <Throwable , JsonObject , Boolean > exceptionMiddleware
6973 ) {
7074 this .queue = new EventQueue (this ::sendEventBatch , flushInterval , maxBatchSize );
7175 this .gson = gson ;
@@ -88,6 +92,8 @@ public final class PostHogClientImpl implements PostHogClient {
8892 this .allowRemoteFeatureFlagEvaluation = allowRemoteFeatureFlagEvaluation ;
8993 this .sendFeatureFlagEvents = sendFeatureFlagEvents ;
9094 this .featureFlagsRequestTimeout = featureFlagsRequestTimeout ;
95+
96+ this .exceptionMiddleware = exceptionMiddleware ;
9197 }
9298
9399 @ Override
@@ -345,7 +351,6 @@ private boolean trackCapturedFeatureFlagCall(@NotNull String distinctId, @NotNul
345351
346352 // Exceptions
347353
348-
349354 @ Override
350355 public void captureException (@ NotNull Throwable throwable , @ Nullable String distinctId , @ Nullable Object properties ) {
351356 // this function shouldn't ever throw an error, so it logs exceptions instead of allowing them to propagate.
@@ -367,6 +372,9 @@ public void captureException(@NotNull Throwable throwable, @Nullable String dist
367372 eventProps .addProperty (EXCEPTION_PERSON_URL , String .format ("%s/project/%s/person/%s" ,
368373 endpoint , projectApiKey , distinctId ));
369374
375+ if (exceptionMiddleware != null && !exceptionMiddleware .apply (throwable , eventProps ))
376+ return ;
377+
370378 capture (distinctId , EXCEPTION , eventProps );
371379 } catch (Exception e ) {
372380 log .error ("failed to capture exception" , e );
@@ -426,7 +434,7 @@ public void captureException(@NotNull Throwable throwable, @Nullable String dist
426434 // We lie and tell PostHog that this is a Python exception because they don't actually support
427435 // Java yet :) As far as i can tell this is only actually used for syntax highlighting in source
428436 // code (which we don't send) so this is probably OK for now.
429- frame .addProperty ("platform" , "python " );
437+ frame .addProperty ("platform" , "java " );
430438 frame .addProperty ("filename" , element .getFileName ());
431439 frame .addProperty ("abs_path" , element .getFileName ());
432440
0 commit comments