-
-
Notifications
You must be signed in to change notification settings - Fork 359
fix(android): Capture native exceptions consumed by Expo's bridgeless error handling #5871
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
Merged
+579
β1
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
897a02d
fix(android): Capture native exceptions swallowed by Expo's bridgelesβ¦
alwx 421026b
docs: Add changelog entry for Expo bridgeless exception capture
alwx 210681f
Merge branch 'main' into alwx/improvement/5868
alwx 9567e56
fix(android): Address PR review feedback for Expo exception handler
alwx 157ed88
Expo stubs in a separate project; fixes
alwx 9c01257
Fixes
alwx 34008f8
Merge branch 'main' into alwx/improvement/5868
alwx bac2288
Merge branch 'main' into alwx/improvement/5868
alwx 5811daf
Fix
alwx 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
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
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 |
|---|---|---|
|
|
@@ -37,3 +37,4 @@ | |
| !/expo.d.ts | ||
| !/app.plugin.js | ||
| !/plugin/build/**/* | ||
| !/expo-module.config.json | ||
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
96 changes: 96 additions & 0 deletions
96
...yAndroidTester/app/src/test/java/io/sentry/react/expo/SentryReactNativeHostHandlerTest.kt
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,96 @@ | ||
| package io.sentry.react.expo | ||
|
|
||
| import io.sentry.Sentry | ||
| import io.sentry.exception.ExceptionMechanismException | ||
| import org.junit.After | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertNotNull | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.junit.runners.JUnit4 | ||
| import org.mockito.MockedStatic | ||
| import org.mockito.Mockito.mockStatic | ||
| import org.mockito.kotlin.any | ||
| import org.mockito.kotlin.argumentCaptor | ||
| import org.mockito.kotlin.never | ||
| import org.mockito.kotlin.verify | ||
|
|
||
| @RunWith(JUnit4::class) | ||
| class SentryReactNativeHostHandlerTest { | ||
| private var sentryMock: MockedStatic<Sentry>? = null | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| sentryMock?.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun `does not capture when in developer support mode`() { | ||
| sentryMock = | ||
| mockStatic(Sentry::class.java).also { | ||
| it.`when`<Boolean> { Sentry.isEnabled() }.thenReturn(true) | ||
| } | ||
|
|
||
| val handler = SentryReactNativeHostHandler() | ||
| handler.onReactInstanceException(true, RuntimeException("test")) | ||
|
|
||
| sentryMock!!.verify({ Sentry.captureException(any()) }, never()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `does not capture when sentry is not enabled`() { | ||
| sentryMock = | ||
| mockStatic(Sentry::class.java).also { | ||
| it.`when`<Boolean> { Sentry.isEnabled() }.thenReturn(false) | ||
| } | ||
|
|
||
| val handler = SentryReactNativeHostHandler() | ||
| handler.onReactInstanceException(false, RuntimeException("test")) | ||
|
|
||
| sentryMock!!.verify({ Sentry.captureException(any()) }, never()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `captures exception with unhandled mechanism when sentry is enabled`() { | ||
| sentryMock = | ||
| mockStatic(Sentry::class.java).also { | ||
| it.`when`<Boolean> { Sentry.isEnabled() }.thenReturn(true) | ||
| } | ||
|
|
||
| val handler = SentryReactNativeHostHandler() | ||
| val originalException = IllegalStateException("Fabric crash") | ||
|
|
||
| handler.onReactInstanceException(false, originalException) | ||
|
|
||
| val captor = argumentCaptor<Throwable>() | ||
| sentryMock!!.verify { Sentry.captureException(captor.capture()) } | ||
|
|
||
| val captured = captor.firstValue | ||
| assertTrue( | ||
| "Expected ExceptionMechanismException but got ${captured::class.java}", | ||
| captured is ExceptionMechanismException, | ||
| ) | ||
|
|
||
| val mechanismException = captured as ExceptionMechanismException | ||
| val mechanism = mechanismException.exceptionMechanism | ||
| assertEquals("expoReactHost", mechanism.type) | ||
| assertFalse("Mechanism should be unhandled", mechanism.isHandled!!) | ||
| assertEquals(originalException, mechanismException.throwable) | ||
| assertNotNull(mechanismException.thread) | ||
| } | ||
|
|
||
| @Test | ||
| fun `does not throw when sentry capture fails`() { | ||
| sentryMock = | ||
| mockStatic(Sentry::class.java).also { | ||
| it.`when`<Boolean> { Sentry.isEnabled() }.thenReturn(true) | ||
| it.`when`<Any> { Sentry.captureException(any()) }.thenThrow(RuntimeException("Sentry internal error")) | ||
| } | ||
|
|
||
| val handler = SentryReactNativeHostHandler() | ||
| // Should not throw | ||
| handler.onReactInstanceException(false, IllegalStateException("test")) | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| This module provides stubs for `expo-modules-core` interfaces (`Package` and `ReactNativeHostHandler`) needed to compile the Expo-specific source set (`android/src/expo/`). | ||
|
|
||
| The Expo source set registers a `ReactNativeHostHandler` that captures native exceptions swallowed by Expo's bridgeless error handling (`ExpoReactHostDelegate.handleInstanceException`). These stubs are added as a `compileOnly` dependency to `android/build.gradle` (meaning, they are not present at runtime). In Expo projects, the real `expo-modules-core` classes are available at runtime via Expo's autolinking. | ||
|
|
||
| ## Updating the stubs | ||
|
|
||
| To update the stubs, just run `yarn build` from the root of the repo and it will recompile the classes and put them under `packages/core/android/libs/expo-stubs.jar`. Check this newly generated `.jar` in and push. |
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,23 @@ | ||
| allprojects { | ||
| repositories { | ||
| mavenCentral() | ||
| google() | ||
| } | ||
| } | ||
|
|
||
| apply plugin: 'java-library' | ||
|
|
||
| java { | ||
| sourceCompatibility = JavaVersion.VERSION_1_8 | ||
| targetCompatibility = JavaVersion.VERSION_1_8 | ||
| } | ||
|
|
||
| tasks.named('jar', Jar) { | ||
| archiveBaseName.set('expo-stubs') | ||
| archiveVersion.set('') | ||
| destinationDirectory.set(file("$rootDir/../libs")) | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly 'com.google.android:android:4.1.1.4' | ||
| } |
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
packages/core/android/expo-stubs/gradle/wrapper/gradle-wrapper.properties
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,7 @@ | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip | ||
| networkTimeout=10000 | ||
| validateDistributionUrl=true | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.