Skip to content

Commit

Permalink
Set current activity in defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
antonis committed Feb 3, 2025
1 parent 43b26db commit d2fd58f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RNSentryStartTest {
"http://localhost:8969/teststream",
)
val actualOptions = SentryAndroidOptions()
RNSentryStart.getSentryAndroidOptions(actualOptions, options, activity, logger)
RNSentryStart.getSentryAndroidOptions(actualOptions, options, logger)
assert(actualOptions.isEnableSpotlight)
assertEquals("http://localhost:8969/teststream", actualOptions.spotlightConnectionUrl)
}
Expand All @@ -49,7 +49,7 @@ class RNSentryStartTest {
fun `when the spotlight url is passed, the spotlight is enabled for the given url`() {
val options = JavaOnlyMap.of("spotlight", "http://localhost:8969/teststream")
val actualOptions = SentryAndroidOptions()
RNSentryStart.getSentryAndroidOptions(actualOptions, options, activity, logger)
RNSentryStart.getSentryAndroidOptions(actualOptions, options, logger)
assert(actualOptions.isEnableSpotlight)
assertEquals("http://localhost:8969/teststream", actualOptions.spotlightConnectionUrl)
}
Expand All @@ -58,14 +58,14 @@ class RNSentryStartTest {
fun `when the spotlight option is disabled, the spotlight SentryAndroidOption is set to false`() {
val options = JavaOnlyMap.of("spotlight", false)
val actualOptions = SentryAndroidOptions()
RNSentryStart.getSentryAndroidOptions(actualOptions, options, activity, logger)
RNSentryStart.getSentryAndroidOptions(actualOptions, options, logger)
assertFalse(actualOptions.isEnableSpotlight)
}

@Test
fun `the JavascriptException is added to the ignoredExceptionsForType list on with react defaults`() {
val actualOptions = SentryAndroidOptions()
RNSentryStart.updateWithReactDefaults(actualOptions)
RNSentryStart.updateWithReactDefaults(actualOptions, activity)
assertTrue(actualOptions.ignoredExceptionsForType.contains(JavascriptException::class.java))
}

Expand All @@ -79,7 +79,7 @@ class RNSentryStartTest {
"devServerUrl",
"http://localhost:8081",
)
RNSentryStart.getSentryAndroidOptions(options, rnOptions, activity, logger)
RNSentryStart.getSentryAndroidOptions(options, rnOptions, logger)

val breadcrumb =
Breadcrumb().apply {
Expand All @@ -103,7 +103,7 @@ class RNSentryStartTest {
"devServerUrl",
mockDevServerUrl,
)
RNSentryStart.getSentryAndroidOptions(options, rnOptions, activity, logger)
RNSentryStart.getSentryAndroidOptions(options, rnOptions, logger)

val breadcrumb =
Breadcrumb().apply {
Expand All @@ -126,7 +126,7 @@ class RNSentryStartTest {
"devServerUrl",
"http://localhost:8081",
)
RNSentryStart.getSentryAndroidOptions(options, rnOptions, activity, logger)
RNSentryStart.getSentryAndroidOptions(options, rnOptions, logger)

val breadcrumb =
Breadcrumb().apply {
Expand All @@ -142,7 +142,7 @@ class RNSentryStartTest {
@Test
fun `the breadcrumb is not filtered out when the dev server url and dsn are not passed`() {
val options = SentryAndroidOptions()
RNSentryStart.getSentryAndroidOptions(options, JavaOnlyMap(), activity, logger)
RNSentryStart.getSentryAndroidOptions(options, JavaOnlyMap(), logger)

val breadcrumb =
Breadcrumb().apply {
Expand All @@ -159,7 +159,7 @@ class RNSentryStartTest {
fun `the breadcrumb is not filtered out when the dev server url is not passed and the dsn does not match`() {
val options = SentryAndroidOptions()
val rnOptions = JavaOnlyMap.of("dsn", "https://[email protected]/1234567")
RNSentryStart.getSentryAndroidOptions(options, rnOptions, activity, logger)
RNSentryStart.getSentryAndroidOptions(options, rnOptions, logger)

val breadcrumb =
Breadcrumb().apply {
Expand All @@ -176,7 +176,7 @@ class RNSentryStartTest {
fun `the breadcrumb is not filtered out when the dev server url does not match and the dsn is not passed`() {
val options = SentryAndroidOptions()
val rnOptions = JavaOnlyMap.of("devServerUrl", "http://localhost:8081")
RNSentryStart.getSentryAndroidOptions(options, rnOptions, activity, logger)
RNSentryStart.getSentryAndroidOptions(options, rnOptions, logger)

val breadcrumb =
Breadcrumb().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ private RNSentryStart() {
static void startWithConfiguration(
@NotNull final Context context,
@NotNull Sentry.OptionsConfiguration<SentryAndroidOptions> configuration) {
Sentry.OptionsConfiguration<SentryAndroidOptions> defaults =
options -> updateWithReactDefaults(options, null);
RNSentryCompositeOptionsConfiguration compositeConfiguration =
new RNSentryCompositeOptionsConfiguration(
RNSentryStart::updateWithReactDefaults,
configuration,
RNSentryStart::updateWithReactFinals);
defaults, configuration, RNSentryStart::updateWithReactFinals);
SentryAndroid.init(context, compositeConfiguration);
}

Expand All @@ -51,14 +51,13 @@ static void startWithOptions(
@NotNull final ReadableMap rnOptions,
@NotNull Sentry.OptionsConfiguration<SentryAndroidOptions> configuration,
@NotNull ILogger logger) {
Sentry.OptionsConfiguration<SentryAndroidOptions> defaults =
options -> updateWithReactDefaults(options, null);
Sentry.OptionsConfiguration<SentryAndroidOptions> rnConfigurationOptions =
options -> getSentryAndroidOptions(options, rnOptions, null, logger);
options -> getSentryAndroidOptions(options, rnOptions, logger);
RNSentryCompositeOptionsConfiguration compositeConfiguration =
new RNSentryCompositeOptionsConfiguration(
RNSentryStart::updateWithReactDefaults,
rnConfigurationOptions,
configuration,
RNSentryStart::updateWithReactFinals);
defaults, rnConfigurationOptions, configuration, RNSentryStart::updateWithReactFinals);
SentryAndroid.init(context, compositeConfiguration);
}

Expand All @@ -67,21 +66,20 @@ static void startWithOptions(
@NotNull final ReadableMap rnOptions,
@Nullable Activity currentActivity,
@NotNull ILogger logger) {
Sentry.OptionsConfiguration<SentryAndroidOptions> defaults =
options -> updateWithReactDefaults(options, currentActivity);
Sentry.OptionsConfiguration<SentryAndroidOptions> rnConfigurationOptions =
options -> getSentryAndroidOptions(options, rnOptions, currentActivity, logger);
options -> getSentryAndroidOptions(options, rnOptions, logger);
RNSentryCompositeOptionsConfiguration compositeConfiguration =
new RNSentryCompositeOptionsConfiguration(
RNSentryStart::updateWithReactDefaults,
rnConfigurationOptions,
RNSentryStart::updateWithReactFinals);
defaults, rnConfigurationOptions, RNSentryStart::updateWithReactFinals);
SentryAndroid.init(context, compositeConfiguration);
}

static void getSentryAndroidOptions(
@NotNull SentryAndroidOptions options,
@NotNull ReadableMap rnOptions,
@Nullable Activity currentActivity,
ILogger logger) {
@NotNull ILogger logger) {
if (rnOptions.hasKey("debug") && rnOptions.getBoolean("debug")) {
options.setDebug(true);
}
Expand Down Expand Up @@ -193,24 +191,23 @@ static void getSentryAndroidOptions(
}
logger.log(
SentryLevel.INFO, String.format("Native Integrations '%s'", options.getIntegrations()));

setCurrentActivity(currentActivity);
}

/**
* This function updates the options with RNSentry defaults. These default can be overwritten by
* users during manual native initialization.
*/
static void updateWithReactDefaults(@NotNull SentryAndroidOptions options) {
static void updateWithReactDefaults(
@NotNull SentryAndroidOptions options, @Nullable Activity currentActivity) {
@Nullable SdkVersion sdkVersion = options.getSdkVersion();
if (sdkVersion == null) {
sdkVersion = new SdkVersion(RNSentryVersion.ANDROID_SDK_NAME, BuildConfig.VERSION_NAME);
} else {
sdkVersion.setName(RNSentryVersion.ANDROID_SDK_NAME);
}
sdkVersion.addPackage(
RNSentryVersion.REACT_NATIVE_SDK_PACKAGE_NAME,
RNSentryVersion.REACT_NATIVE_SDK_PACKAGE_VERSION);
RNSentryVersion.REACT_NATIVE_SDK_PACKAGE_NAME,
RNSentryVersion.REACT_NATIVE_SDK_PACKAGE_VERSION);

options.setSentryClientName(sdkVersion.getName() + "/" + sdkVersion.getVersion());
options.setNativeSdkName(RNSentryVersion.NATIVE_SDK_NAME);
Expand All @@ -224,6 +221,8 @@ static void updateWithReactDefaults(@NotNull SentryAndroidOptions options) {
// React native internally throws a JavascriptException.
// we want to ignore it on the native side to avoid sending it twice.
options.addIgnoredExceptionForType(JavascriptException.class);

setCurrentActivity(currentActivity);
}

/**
Expand Down

0 comments on commit d2fd58f

Please sign in to comment.