From 17a41c14c3928588cb52b7d8546e787c473d8850 Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Wed, 13 Nov 2024 21:43:41 +0100 Subject: [PATCH] Ensure android initialization process continues even if options configuration block throws an exception (#3887) * Ensure android event processors are added even if options configuration block throws * Changelog --- CHANGELOG.md | 6 ++++++ .../java/io/sentry/android/core/SentryAndroid.java | 12 +++++++++++- .../io/sentry/android/core/SentryAndroidTest.kt | 13 +++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7a41e65dc..98595beaa8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Ensure android initialization process continues even if options configuration block throws an exception ([#3887](https://github.com/getsentry/sentry-java/pull/3887)) + ## 7.17.0 ### Features diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java index e6e677334c..adeb451332 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java @@ -129,7 +129,17 @@ public static synchronized void init( isTimberAvailable, isReplayAvailable); - configuration.configure(options); + try { + configuration.configure(options); + } catch (Throwable t) { + // let it slip, but log it + options + .getLogger() + .log( + SentryLevel.ERROR, + "Error in the 'OptionsConfiguration.configure' callback.", + t); + } // if SentryPerformanceProvider was disabled or removed, // we set the app start / sdk init time here instead diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidTest.kt index c31076d1ff..17c11475c9 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidTest.kt @@ -517,6 +517,19 @@ class SentryAndroidTest { assertEquals(99, AppStartMetrics.getInstance().appStartTimeSpan.startUptimeMs) } + @Test + fun `if the config options block throws still intializes android event processors`() { + lateinit var optionsRef: SentryOptions + fixture.initSut(context = mock()) { options -> + optionsRef = options + options.dsn = "https://key@sentry.io/123" + throw RuntimeException("Boom!") + } + + assertTrue(optionsRef.eventProcessors.any { it is DefaultAndroidEventProcessor }) + assertTrue(optionsRef.eventProcessors.any { it is AnrV2EventProcessor }) + } + private fun prefillScopeCache(cacheDir: String) { val scopeDir = File(cacheDir, SCOPE_CACHE).also { it.mkdirs() } File(scopeDir, BREADCRUMBS_FILENAME).writeText(