-
Notifications
You must be signed in to change notification settings - Fork 41
feat(errortracking): ignoredExceptionTypes config to skip RN-duplicate JS errors #569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tsushanth
wants to merge
2
commits into
PostHog:main
Choose a base branch
from
tsushanth:fix/issue-567-ignored-exception-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−200
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "posthog": minor | ||
| --- | ||
|
|
||
| Add `errorTrackingConfig.ignoredExceptionTypes`, a `MutableList<Class<out Throwable>>` consulted by `PostHog.captureException` (both autocapture and direct callers). The throwable and every entry in its cause chain are tested with `Class.isInstance`; if any link matches, the SDK skips the `$exception` event. Matching by `Class` reference is safe under R8 / ProGuard renames. The downstream uncaught-exception handler still runs. Defaults to empty. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,8 +69,6 @@ internal class PostHogTest { | |
| evaluationContexts: List<String>? = null, | ||
| context: PostHogContext? = null, | ||
| personProfiles: PersonProfiles = PersonProfiles.IDENTIFIED_ONLY, | ||
| exceptionStepsEnabled: Boolean = true, | ||
| exceptionStepsMaxBytes: Int = 32768, | ||
| ): PostHogInterface { | ||
| config = | ||
| PostHogConfig(API_KEY, host).apply { | ||
|
|
@@ -94,8 +92,6 @@ internal class PostHogTest { | |
| addBeforeSend(beforeSend) | ||
| } | ||
| this.errorTrackingConfig.inAppIncludes.add("com.posthog") | ||
| this.errorTrackingConfig.exceptionSteps.enabled = exceptionStepsEnabled | ||
| this.errorTrackingConfig.exceptionSteps.maxBytes = exceptionStepsMaxBytes | ||
|
Comment on lines
-97
to
-98
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| this.context = context | ||
| this.personProfiles = personProfiles | ||
| } | ||
|
|
@@ -2541,6 +2537,69 @@ internal class PostHogTest { | |
| sut.close() | ||
| } | ||
|
|
||
| /** | ||
| * Local stand-in for `com.facebook.react.common.JavascriptException` (the | ||
| * production target). Using a class defined here lets the test register | ||
| * the same `Class` reference at config time and at capture time without | ||
| * pulling React Native onto the classpath. | ||
| */ | ||
| private class JavascriptExceptionStub(message: String) : RuntimeException(message) | ||
|
|
||
| @Test | ||
| fun `captureException drops events whose throwable matches ignoredExceptionTypes`() { | ||
| val http = mockHttp() | ||
| val url = http.url("/") | ||
|
|
||
| val sut = getSut(url.toString(), preloadFeatureFlags = false) | ||
| config.errorTrackingConfig.ignoredExceptionTypes.add(JavascriptExceptionStub::class.java) | ||
|
|
||
| sut.captureException(JavascriptExceptionStub("Unhandled JS Exception")) | ||
|
|
||
| queueExecutor.shutdownAndAwaitTermination() | ||
| assertEquals(0, http.requestCount) | ||
|
|
||
| sut.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun `captureException drops events whose cause chain contains an ignored type`() { | ||
| val http = mockHttp() | ||
| val url = http.url("/") | ||
|
|
||
| val sut = getSut(url.toString(), preloadFeatureFlags = false) | ||
| config.errorTrackingConfig.ignoredExceptionTypes.add(JavascriptExceptionStub::class.java) | ||
|
|
||
| // PostHogThrowable / RuntimeException wrappers must not defeat the filter: | ||
| // the cause chain still surfaces the ignored type. | ||
| val wrapped = | ||
| RuntimeException( | ||
| "wrapper", | ||
| JavascriptExceptionStub("Unhandled JS Exception"), | ||
| ) | ||
| sut.captureException(wrapped) | ||
|
|
||
| queueExecutor.shutdownAndAwaitTermination() | ||
| assertEquals(0, http.requestCount) | ||
|
|
||
| sut.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun `captureException keeps events whose throwable is not in ignoredExceptionTypes`() { | ||
| val http = mockHttp() | ||
| val url = http.url("/") | ||
|
|
||
| val sut = getSut(url.toString(), preloadFeatureFlags = false) | ||
| config.errorTrackingConfig.ignoredExceptionTypes.add(JavascriptExceptionStub::class.java) | ||
|
|
||
| sut.captureException(IllegalStateException("normal failure")) | ||
|
|
||
| queueExecutor.shutdownAndAwaitTermination() | ||
| assertEquals(1, http.requestCount) | ||
|
|
||
| sut.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun `sets default person properties on SDK setup when enabled`() { | ||
| val http = | ||
|
|
@@ -3381,199 +3440,4 @@ internal class PostHogTest { | |
|
|
||
| sut.close() | ||
| } | ||
|
|
||
| private fun exceptionStepMessages(event: com.posthog.PostHogEvent): List<Any?> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests should not be removed here |
||
| @Suppress("UNCHECKED_CAST") | ||
| val steps = event.properties!!["\$exception_steps"] as? List<Map<String, Any>> ?: return emptyList() | ||
| return steps.map { it["\$message"] } | ||
| } | ||
|
|
||
| @Test | ||
| fun `addExceptionStep attaches ordered steps to the exception event`() { | ||
| val http = mockHttp() | ||
| val url = http.url("/") | ||
|
|
||
| val sut = getSut(url.toString(), preloadFeatureFlags = false, reloadFeatureFlags = false) | ||
|
|
||
| sut.addExceptionStep("A", mapOf("screen" to "cart")) | ||
| sut.addExceptionStep("B") | ||
| sut.addExceptionStep("C") | ||
|
|
||
| sut.captureException(RuntimeException("boom")) | ||
|
|
||
| queueExecutor.shutdownAndAwaitTermination() | ||
|
|
||
| val request = http.takeRequest() | ||
| val content = request.body.unGzip() | ||
| val batch = serializer.deserialize<PostHogBatchEvent>(content.reader()) | ||
|
|
||
| val theEvent = batch.batch.first() | ||
| assertEquals("\$exception", theEvent.event) | ||
| assertEquals(listOf("A", "B", "C"), exceptionStepMessages(theEvent)) | ||
|
|
||
| sut.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun `addExceptionStep does not overwrite caller-provided exception steps`() { | ||
| val http = mockHttp() | ||
| val url = http.url("/") | ||
|
|
||
| val sut = getSut(url.toString(), preloadFeatureFlags = false, reloadFeatureFlags = false) | ||
|
|
||
| sut.addExceptionStep("buffered") | ||
|
|
||
| sut.captureException( | ||
| RuntimeException("boom"), | ||
| properties = mapOf("\$exception_steps" to listOf(mapOf("\$message" to "caller"))), | ||
| ) | ||
|
|
||
| queueExecutor.shutdownAndAwaitTermination() | ||
|
|
||
| val request = http.takeRequest() | ||
| val content = request.body.unGzip() | ||
| val batch = serializer.deserialize<PostHogBatchEvent>(content.reader()) | ||
|
|
||
| assertEquals(listOf("caller"), exceptionStepMessages(batch.batch.first())) | ||
|
|
||
| sut.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun `exception steps persist across captures and identity changes`() { | ||
| val http = mockHttp() | ||
| val url = http.url("/") | ||
|
|
||
| val sut = getSut(url.toString(), preloadFeatureFlags = false, reloadFeatureFlags = false, flushAt = 100) | ||
|
|
||
| sut.addExceptionStep("A") | ||
| sut.addExceptionStep("B") | ||
| sut.captureException(RuntimeException("first")) | ||
|
|
||
| sut.reset() | ||
|
|
||
| sut.addExceptionStep("C") | ||
| sut.captureException(RuntimeException("second")) | ||
|
|
||
| // flushAt is high so neither capture triggers a flush on its own; flush explicitly | ||
| // so both events land in a single batch | ||
| sut.flush() | ||
| queueExecutor.shutdownAndAwaitTermination() | ||
|
|
||
| val request = http.takeRequest() | ||
| val content = request.body.unGzip() | ||
| val batch = serializer.deserialize<PostHogBatchEvent>(content.reader()) | ||
|
|
||
| val exceptions = batch.batch.filter { it.event == "\$exception" } | ||
| assertEquals(listOf("A", "B"), exceptionStepMessages(exceptions[0])) | ||
| assertEquals(listOf("A", "B", "C"), exceptionStepMessages(exceptions[1])) | ||
|
|
||
| sut.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun `addExceptionStep is a no-op when exception steps are disabled`() { | ||
| val http = mockHttp() | ||
| val url = http.url("/") | ||
|
|
||
| val sut = | ||
| getSut( | ||
| url.toString(), | ||
| preloadFeatureFlags = false, | ||
| reloadFeatureFlags = false, | ||
| exceptionStepsEnabled = false, | ||
| ) | ||
|
|
||
| sut.addExceptionStep("A") | ||
| sut.captureException(RuntimeException("boom")) | ||
|
|
||
| queueExecutor.shutdownAndAwaitTermination() | ||
|
|
||
| val request = http.takeRequest() | ||
| val content = request.body.unGzip() | ||
| val batch = serializer.deserialize<PostHogBatchEvent>(content.reader()) | ||
|
|
||
| assertTrue(exceptionStepMessages(batch.batch.first()).isEmpty()) | ||
|
|
||
| sut.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun `addExceptionStep is a no-op when maxBytes is not positive`() { | ||
| val http = mockHttp() | ||
| val url = http.url("/") | ||
|
|
||
| val sut = | ||
| getSut( | ||
| url.toString(), | ||
| preloadFeatureFlags = false, | ||
| reloadFeatureFlags = false, | ||
| exceptionStepsMaxBytes = 0, | ||
| ) | ||
|
|
||
| sut.addExceptionStep("A") | ||
| sut.captureException(RuntimeException("boom")) | ||
|
|
||
| queueExecutor.shutdownAndAwaitTermination() | ||
|
|
||
| val request = http.takeRequest() | ||
| val content = request.body.unGzip() | ||
| val batch = serializer.deserialize<PostHogBatchEvent>(content.reader()) | ||
|
|
||
| assertTrue(exceptionStepMessages(batch.batch.first()).isEmpty()) | ||
|
|
||
| sut.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun `addExceptionStep clears the buffer on optOut and stops buffering while opted out`() { | ||
| val http = mockHttp() | ||
| val url = http.url("/") | ||
|
|
||
| val sut = getSut(url.toString(), preloadFeatureFlags = false, reloadFeatureFlags = false) | ||
|
|
||
| sut.addExceptionStep("before opt-out") | ||
| sut.optOut() | ||
| sut.addExceptionStep("while opted out") | ||
| sut.optIn() | ||
|
|
||
| sut.captureException(RuntimeException("boom")) | ||
|
|
||
| queueExecutor.shutdownAndAwaitTermination() | ||
|
|
||
| val request = http.takeRequest() | ||
| val content = request.body.unGzip() | ||
| val batch = serializer.deserialize<PostHogBatchEvent>(content.reader()) | ||
|
|
||
| assertTrue(exceptionStepMessages(batch.batch.first()).isEmpty()) | ||
|
|
||
| sut.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun `exception steps attach to a generic capture of the exception event`() { | ||
| // Hybrid SDKs (RN/Flutter) forward steps via addExceptionStep and emit the | ||
| // exception through the generic capture() entry point rather than captureException. | ||
| val http = mockHttp() | ||
| val url = http.url("/") | ||
|
|
||
| val sut = getSut(url.toString(), preloadFeatureFlags = false, reloadFeatureFlags = false) | ||
|
|
||
| sut.addExceptionStep("A") | ||
| sut.addExceptionStep("B") | ||
|
|
||
| sut.capture("\$exception", properties = mapOf("\$exception_message" to "boom")) | ||
|
|
||
| queueExecutor.shutdownAndAwaitTermination() | ||
|
|
||
| val request = http.takeRequest() | ||
| val content = request.body.unGzip() | ||
| val batch = serializer.deserialize<PostHogBatchEvent>(content.reader()) | ||
|
|
||
| val theEvent = batch.batch.first() | ||
| assertEquals("\$exception", theEvent.event) | ||
| assertEquals(listOf("A", "B"), exceptionStepMessages(theEvent)) | ||
|
|
||
| sut.close() | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same, probably stale commits from main