diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AppState.java b/sentry-android-core/src/main/java/io/sentry/android/core/AppState.java index 24562e3aa49..85ed5f78ff0 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AppState.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AppState.java @@ -112,7 +112,8 @@ private void ensureLifecycleObserver(final @NotNull ILogger logger) { SentryLevel.WARNING, "androidx.lifecycle is not available, some features might not be properly working," + "e.g. Session Tracking, Network and System Events breadcrumbs, etc."); - } catch (Throwable e) { + } catch (LinkageError e) { + // androidx.lifecycle has an incompatible version logger.log(SentryLevel.ERROR, "AppState could not register lifecycle observer", e); } } @@ -124,7 +125,7 @@ private void addObserverInternal(final @NotNull ILogger logger) { if (observerRef != null) { ProcessLifecycleOwner.get().getLifecycle().addObserver(observerRef); } - } catch (Throwable e) { + } catch (LinkageError e) { // This is to handle a potential 'AbstractMethodError' gracefully. The error is triggered in // connection with conflicting dependencies of the androidx.lifecycle. // //See the issue here: https://github.com/getsentry/sentry-java/pull/2228 diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java index f8c96b71459..acabb77d493 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java @@ -88,7 +88,9 @@ public final void register(final @NotNull IScopes scopes, final @NotNull SentryO this.options .getLogger() .log(SentryLevel.ERROR, "Failed to invoke the SentryNdk.init method.", e); - } catch (Throwable e) { + } catch (ReflectiveOperationException e) { + // InvocationTargetException wraps whatever SentryNdk.init threw, such as an + // UnsatisfiedLinkError when the native library failed to load disableNdkIntegration(this.options); this.options.getLogger().log(SentryLevel.ERROR, "Failed to initialize SentryNdk.", e); } @@ -120,7 +122,7 @@ public void close() throws IOException { options .getLogger() .log(SentryLevel.ERROR, "Failed to invoke the SentryNdk.close method.", e); - } catch (Throwable e) { + } catch (ReflectiveOperationException e) { options.getLogger().log(SentryLevel.ERROR, "Failed to close SentryNdk.", e); } finally { disableNdkIntegration(this.options); diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/ViewHierarchyEventProcessor.java b/sentry-android-core/src/main/java/io/sentry/android/core/ViewHierarchyEventProcessor.java index 4004310a3ff..99fe0d98cd4 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/ViewHierarchyEventProcessor.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/ViewHierarchyEventProcessor.java @@ -189,7 +189,14 @@ public static ViewHierarchy snapshotViewHierarchy( try { viewHierarchy.set(snapshotViewHierarchy(decorView, exporters)); latch.countDown(); + } catch (LinkageError e) { + // The Compose exporter calls Compose internals, which may not match the app's + // Compose version + logger.log(SentryLevel.ERROR, "Failed to process view hierarchy.", e); } catch (Throwable t) { + // The snapshot calls into the app's own views, so we can't know what it throws. + // The event is sent without a view hierarchy. + ExceptionUtils.rethrowIfFatal(t); logger.log(SentryLevel.ERROR, "Failed to process view hierarchy.", t); } }); @@ -197,7 +204,14 @@ public static ViewHierarchy snapshotViewHierarchy( return viewHierarchy.get(); } } + } catch (LinkageError e) { + // The Compose exporter calls Compose internals, which may not match the app's Compose + // version + logger.log(SentryLevel.ERROR, "Failed to process view hierarchy.", e); } catch (Throwable t) { + // The snapshot calls into the app's own views, so we can't know what it throws. The event + // is sent without a view hierarchy. + ExceptionUtils.rethrowIfFatal(t); logger.log(SentryLevel.ERROR, "Failed to process view hierarchy.", t); } return null; diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryWindowCallback.java b/sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryWindowCallback.java index 612eb97946e..7a3e6b42302 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryWindowCallback.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/internal/gestures/SentryWindowCallback.java @@ -6,6 +6,7 @@ import io.sentry.SentryLevel; import io.sentry.SentryOptions; import io.sentry.SpanStatus; +import io.sentry.util.ExceptionUtils; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -56,7 +57,16 @@ public boolean dispatchTouchEvent(final @Nullable MotionEvent motionEvent) { final MotionEvent copy = motionEventObtainer.obtain(motionEvent); try { handleTouchEvent(copy); + } catch (LinkageError e) { + // The Compose gesture locator calls Compose internals, which may not match the app's + // Compose version + if (options != null) { + options.getLogger().log(SentryLevel.ERROR, "Error dispatching touch event", e); + } } catch (Throwable e) { + // Locating the touched view calls into the app's own views, so we can't know what it + // throws. The gesture is not recorded, and the touch still reaches the app. + ExceptionUtils.rethrowIfFatal(e); if (options != null) { options.getLogger().log(SentryLevel.ERROR, "Error dispatching touch event", e); } diff --git a/sentry-android-ndk/src/main/java/io/sentry/android/ndk/DebugImagesLoader.java b/sentry-android-ndk/src/main/java/io/sentry/android/ndk/DebugImagesLoader.java index 602981c1d01..af817abe722 100644 --- a/sentry-android-ndk/src/main/java/io/sentry/android/ndk/DebugImagesLoader.java +++ b/sentry-android-ndk/src/main/java/io/sentry/android/ndk/DebugImagesLoader.java @@ -8,6 +8,7 @@ import io.sentry.ndk.NativeModuleListLoader; import io.sentry.protocol.DebugImage; import io.sentry.util.AutoClosableReentrantLock; +import io.sentry.util.ExceptionUtils; import io.sentry.util.Objects; import java.util.ArrayList; import java.util.HashSet; @@ -71,7 +72,13 @@ public DebugImagesLoader( .getLogger() .log(SentryLevel.DEBUG, "Debug images loaded: %d", debugImages.size()); } + } catch (LinkageError e) { + // e.g. UnsatisfiedLinkError when the native library failed to load + options.getLogger().log(SentryLevel.ERROR, e, "Failed to load debug images."); } catch (Throwable e) { + // A JNI call into sentry-native, so we can't know what it throws. The event is sent + // without debug images. + ExceptionUtils.rethrowIfFatal(e); options.getLogger().log(SentryLevel.ERROR, e, "Failed to load debug images."); } } @@ -172,7 +179,13 @@ public void clearDebugImages() { moduleListLoader.clearModuleList(); options.getLogger().log(SentryLevel.INFO, "Debug images cleared."); + } catch (LinkageError e) { + // e.g. UnsatisfiedLinkError when the native library failed to load + options.getLogger().log(SentryLevel.ERROR, e, "Failed to clear debug images."); } catch (Throwable e) { + // A JNI call into sentry-native, so we can't know what it throws. The cached list is + // cleared either way. + ExceptionUtils.rethrowIfFatal(e); options.getLogger().log(SentryLevel.ERROR, e, "Failed to clear debug images."); } debugImages = null; diff --git a/sentry-android-ndk/src/main/java/io/sentry/android/ndk/SentryNdk.java b/sentry-android-ndk/src/main/java/io/sentry/android/ndk/SentryNdk.java index e2339ca911d..e88f1435ab1 100644 --- a/sentry-android-ndk/src/main/java/io/sentry/android/ndk/SentryNdk.java +++ b/sentry-android-ndk/src/main/java/io/sentry/android/ndk/SentryNdk.java @@ -25,9 +25,9 @@ private SentryNdk() {} try { //noinspection UnstableApiUsage io.sentry.ndk.SentryNdk.loadNativeLibraries(); - } catch (Throwable t) { - // ignored - // if loadLibrary() fails, the later init() will throw an exception anyway + } catch (LinkageError ignored) { + // e.g. UnsatisfiedLinkError. The later init() throws too, which disables the NDK + // integration. } finally { loadLibraryLatch.countDown(); } diff --git a/sentry-android-replay/src/main/java/io/sentry/android/replay/Windows.kt b/sentry-android-replay/src/main/java/io/sentry/android/replay/Windows.kt index 4f8f6cabf58..d38653324f4 100644 --- a/sentry-android-replay/src/main/java/io/sentry/android/replay/Windows.kt +++ b/sentry-android-replay/src/main/java/io/sentry/android/replay/Windows.kt @@ -23,6 +23,7 @@ import android.util.Log import android.view.View import android.view.Window import io.sentry.util.AutoClosableReentrantLock +import io.sentry.util.ExceptionUtils import java.io.Closeable import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.atomic.AtomicBoolean @@ -53,7 +54,7 @@ internal object WindowSpy { lazy(NONE) { try { Class.forName("com.android.internal.policy.DecorView") - } catch (ignored: Throwable) { + } catch (ignored: ClassNotFoundException) { Log.d("WindowSpy", "Unexpected exception loading DecorView on API $SDK_INT", ignored) null } @@ -171,7 +172,7 @@ internal object WindowManagerSpy { val className = "android.view.WindowManagerGlobal" try { Class.forName(className) - } catch (ignored: Throwable) { + } catch (ignored: ClassNotFoundException) { Log.w("WindowManagerSpy", ignored) null } @@ -195,11 +196,30 @@ internal object WindowManagerSpy { mViewsField?.let { mViewsField -> @Suppress("UNCHECKED_CAST") val mViews = mViewsField[windowManagerInstance] as ArrayList - mViewsField[windowManagerInstance] = swap(mViews) + val swapped = swapSafely(swap, mViews) ?: return + mViewsField[windowManagerInstance] = swapped } } - } catch (ignored: Throwable) { + } catch (ignored: ReflectiveOperationException) { + Log.w("WindowManagerSpy", ignored) + } catch (ignored: ClassCastException) { + // mViews is not an ArrayList on this device Log.w("WindowManagerSpy", ignored) } } + + private fun swapSafely( + swap: (ArrayList) -> ArrayList, + mViews: ArrayList, + ): ArrayList? = + try { + swap(mViews) + } catch (@Suppress("TooGenericExceptionCaught") e: Throwable) { + // swap notifies the root view listeners about the existing views, and those run replay's + // recorders, so we can't know what they throw. mViews is left as it was, so replay doesn't + // learn about root views being added or removed. + ExceptionUtils.rethrowIfFatal(e) + Log.w("WindowManagerSpy", e) + null + } } diff --git a/sentry-android-replay/src/main/java/io/sentry/android/replay/util/Nodes.kt b/sentry-android-replay/src/main/java/io/sentry/android/replay/util/Nodes.kt index 704260cf311..66372e7ba77 100644 --- a/sentry-android-replay/src/main/java/io/sentry/android/replay/util/Nodes.kt +++ b/sentry-android-replay/src/main/java/io/sentry/android/replay/util/Nodes.kt @@ -33,6 +33,7 @@ import androidx.compose.ui.layout.LayoutCoordinates import androidx.compose.ui.layout.findRootCoordinates import androidx.compose.ui.node.LayoutNode import androidx.compose.ui.text.TextLayoutResult +import io.sentry.util.ExceptionUtils import kotlin.math.ceil import kotlin.math.floor import kotlin.math.roundToInt @@ -93,7 +94,7 @@ internal fun LayoutNode.findPainter(): Painter? { return try { modifier::class.java.getDeclaredField("painter").apply { isAccessible = true }.get(modifier) as? Painter - } catch (e: Throwable) { + } catch (_: ReflectiveOperationException) { null } } @@ -136,7 +137,12 @@ internal fun LayoutNode.findTextColor(): Color? { (modifier::class.java.getDeclaredField("color").apply { isAccessible = true }.get(modifier) as? ColorProducer) ?.invoke() + } catch (_: LinkageError) { + // older Compose versions don't have ColorProducer + null } catch (e: Throwable) { + // ColorProducer.invoke() runs the app's color lambda, so we can't know what it throws + ExceptionUtils.rethrowIfFatal(e) null } } diff --git a/sentry-android-replay/src/main/java/io/sentry/android/replay/viewhierarchy/ComposeViewHierarchyNode.kt b/sentry-android-replay/src/main/java/io/sentry/android/replay/viewhierarchy/ComposeViewHierarchyNode.kt index 2e40144e2de..cf8b8c69eac 100644 --- a/sentry-android-replay/src/main/java/io/sentry/android/replay/viewhierarchy/ComposeViewHierarchyNode.kt +++ b/sentry-android-replay/src/main/java/io/sentry/android/replay/viewhierarchy/ComposeViewHierarchyNode.kt @@ -32,6 +32,7 @@ import io.sentry.android.replay.util.toRect import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode.GenericViewHierarchyNode import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode.ImageViewHierarchyNode import io.sentry.android.replay.viewhierarchy.ViewHierarchyNode.TextViewHierarchyNode +import io.sentry.util.ExceptionUtils import java.lang.ref.WeakReference import java.lang.reflect.Method @@ -45,7 +46,7 @@ internal object ComposeViewHierarchyNode { .java .getDeclaredMethod("getCollapsedSemantics\$ui_release") .apply { isAccessible = true } - } catch (_: Throwable) { + } catch (_: NoSuchMethodException) { // ignore, as this method may not be available } return@lazy null @@ -57,7 +58,7 @@ internal object ComposeViewHierarchyNode { internal fun retrieveSemanticsConfiguration(node: LayoutNode): SemanticsConfiguration? { return try { node.semanticsConfiguration - } catch (t: Throwable) { + } catch (t: NoSuchMethodError) { // for backwards compatibility // Jetpack Compose 1.8 or older if (getCollapsedSemanticsMethod != null) { @@ -128,16 +129,27 @@ internal object ComposeViewHierarchyNode { } val visibleRect = node.coordinates.boundsInWindow(_rootCoordinates?.get()) - val semantics: SemanticsConfiguration? + var semanticsFailure: Throwable? = null + val semantics: SemanticsConfiguration? = + try { + retrieveSemanticsConfiguration(node) + } catch (e: LinkageError) { + // an unsupported Compose version, which must not crash the app + semanticsFailure = e + null + } catch (t: Throwable) { + // semantics modifiers run app code, so we can't know what this throws + ExceptionUtils.rethrowIfFatal(t) + semanticsFailure = t + null + } - try { - semantics = retrieveSemanticsConfiguration(node) - } catch (t: Throwable) { + if (semanticsFailure != null) { if (!semanticsRetrievalErrorLogged) { semanticsRetrievalErrorLogged = true logger.log( SentryLevel.ERROR, - t, + semanticsFailure, """ Error retrieving semantics information from Compose tree. Most likely you're using an unsupported version of androidx.compose.ui:ui. The supported @@ -152,7 +164,7 @@ internal object ComposeViewHierarchyNode { // fail fast in our own sample/UI-test apps (see SentryReplayDebug), so regressions surface // as crashes instead of silently degrading masking if (SentryReplayDebug.failFast) { - throw t + throw semanticsFailure } // If we're unable to retrieve the semantics configuration @@ -284,13 +296,23 @@ internal object ComposeViewHierarchyNode { return false } + var traversalFailure: Throwable? = null try { val rootNode = (view as? Owner)?.root ?: return false rootNode.traverse(parent, isComposeRoot = true, options, logger) + } catch (e: LinkageError) { + // an unsupported Compose version, which must not crash the app + traversalFailure = e } catch (e: Throwable) { + // the traversal runs app code, so we can't know what it throws + ExceptionUtils.rethrowIfFatal(e) + traversalFailure = e + } + + if (traversalFailure != null) { logger.log( SentryLevel.ERROR, - e, + traversalFailure, """ Error traversing Compose tree. Most likely you're using an unsupported version of androidx.compose.ui:ui. The minimum supported version is 1.5.0. If it's a newer @@ -302,7 +324,7 @@ internal object ComposeViewHierarchyNode { // fail fast in our own sample/UI-test apps (see SentryReplayDebug), so regressions surface // as crashes instead of silently skipping the whole Compose subtree (i.e. not masking it) if (SentryReplayDebug.failFast) { - throw e + throw traversalFailure } return false } diff --git a/sentry-android-sqlite/src/main/java/io/sentry/sqlite/SentrySQLiteDriver.kt b/sentry-android-sqlite/src/main/java/io/sentry/sqlite/SentrySQLiteDriver.kt index 28b661cd3e7..baf4c6d6430 100644 --- a/sentry-android-sqlite/src/main/java/io/sentry/sqlite/SentrySQLiteDriver.kt +++ b/sentry-android-sqlite/src/main/java/io/sentry/sqlite/SentrySQLiteDriver.kt @@ -4,6 +4,7 @@ import androidx.sqlite.SQLiteConnection import androidx.sqlite.SQLiteDriver import io.sentry.ScopesAdapter import io.sentry.SentryLevel +import io.sentry.util.ExceptionUtils import io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion /** @@ -53,19 +54,31 @@ public class SentrySQLiteDriver private constructor(private val delegate: SQLite // create() ensures delegate is unwrapped, so we don't need to protect against double-wrapping // the connection. SentrySQLiteConnection(connection, spans) + } catch (e: LinkageError) { + // androidx.sqlite is compileOnly, so the app's version may not match the SQLiteConnection + // that SentrySQLiteConnection implements. The connection is returned uninstrumented. + logInstrumentationFailure(e) + connection } catch (t: Throwable) { - ScopesAdapter.getInstance() - .options - .logger - .log( - SentryLevel.ERROR, - "Failed to instrument SQLite connection; returning uninstrumented connection.", - t, - ) + // Instrumenting must never stop the app from opening its database, so any other failure + // also returns the connection uninstrumented. + ExceptionUtils.rethrowIfFatal(t) + logInstrumentationFailure(t) connection } } + private fun logInstrumentationFailure(t: Throwable) { + ScopesAdapter.getInstance() + .options + .logger + .log( + SentryLevel.ERROR, + "Failed to instrument SQLite connection; returning uninstrumented connection.", + t, + ) + } + public companion object { /** diff --git a/sentry-compose/src/androidMain/kotlin/io/sentry/compose/SentryComposeHelper.kt b/sentry-compose/src/androidMain/kotlin/io/sentry/compose/SentryComposeHelper.kt index 10d02103ba1..39325bda05e 100644 --- a/sentry-compose/src/androidMain/kotlin/io/sentry/compose/SentryComposeHelper.kt +++ b/sentry-compose/src/androidMain/kotlin/io/sentry/compose/SentryComposeHelper.kt @@ -35,8 +35,10 @@ internal class SentryComposeHelper(logger: ILogger) { val value = sentryTagElementField.get(modifier) return value as String? } - } catch (e: Throwable) { - // ignored + } catch (_: IllegalAccessException) { + // fall back to SemanticsModifier below + } catch (_: ClassCastException) { + // the tag is not a String in this Compose version } // Older versions use SemanticsModifier diff --git a/sentry-opentelemetry/sentry-opentelemetry-bootstrap/src/main/java/io/sentry/opentelemetry/SentryContextStorageProvider.java b/sentry-opentelemetry/sentry-opentelemetry-bootstrap/src/main/java/io/sentry/opentelemetry/SentryContextStorageProvider.java index 16599595866..45dd6026874 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-bootstrap/src/main/java/io/sentry/opentelemetry/SentryContextStorageProvider.java +++ b/sentry-opentelemetry/sentry-opentelemetry-bootstrap/src/main/java/io/sentry/opentelemetry/SentryContextStorageProvider.java @@ -2,6 +2,7 @@ import io.opentelemetry.context.ContextStorage; import io.opentelemetry.context.ContextStorageProvider; +import io.sentry.util.ExceptionUtils; import java.util.Iterator; import java.util.ServiceLoader; import org.jetbrains.annotations.NotNull; @@ -23,8 +24,11 @@ public ContextStorage get() { return contextStorageProvider.get(); } } + } catch (LinkageError ignored) { + // a provider needs a class that is missing or has an incompatible version, use fallback } catch (Throwable t) { - // ignore and use fallback + // Other providers are third-party code, so we can't know what they throw. Use fallback. + ExceptionUtils.rethrowIfFatal(t); } // using default / fallback storage diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index 75c263c4127..927b2b6f117 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -9,6 +9,7 @@ import io.sentry.metrics.MetricsApi; import io.sentry.protocol.*; import io.sentry.transport.RateLimiter; +import io.sentry.util.ExceptionUtils; import io.sentry.util.HintUtils; import io.sentry.util.Objects; import io.sentry.util.SpanUtils; @@ -417,7 +418,16 @@ public void close(final boolean isRestarting) { if (integration instanceof Closeable) { try { ((Closeable) integration).close(); + } catch (LinkageError e) { + // The integration needs an optional dependency that is missing or has an + // incompatible version. The remaining integrations are still closed. + getOptions() + .getLogger() + .log(SentryLevel.WARNING, "Failed to close the integration {}.", integration, e); } catch (Throwable e) { + // Integrations can be user-provided, so we can't know what they throw. The + // remaining integrations are still closed. + ExceptionUtils.rethrowIfFatal(e); getOptions() .getLogger() .log(SentryLevel.WARNING, "Failed to close the integration {}.", integration, e); @@ -476,6 +486,9 @@ public void close(final boolean isRestarting) { configureScope(ScopeType.ISOLATION, scope -> scope.getClient().close(isRestarting)); configureScope(ScopeType.GLOBAL, scope -> scope.getClient().close(isRestarting)); } catch (Throwable e) { + // A public API entry point: closing shuts down components that integrations and the app + // can replace, so we can't know what it throws. The remaining shutdown steps are skipped. + ExceptionUtils.rethrowIfFatal(e); getOptions().getLogger().log(SentryLevel.ERROR, "Error while closing the Scopes.", e); } } diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index ce286600477..7f7879864e5 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -21,6 +21,7 @@ import io.sentry.transport.NoOpEnvelopeCache; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.DebugMetaPropertiesApplier; +import io.sentry.util.ExceptionUtils; import io.sentry.util.FileUtils; import io.sentry.util.InitUtil; import io.sentry.util.LoadClass; @@ -362,7 +363,21 @@ private static void init(final @NotNull SentryOptions options, final boolean glo for (final Integration integration : options.getIntegrations()) { try { integration.register(ScopesAdapter.getInstance(), options); + } catch (LinkageError e) { + // The integration needs an optional dependency that is missing or has an incompatible + // version. It is skipped and init continues. + options + .getLogger() + .log( + SentryLevel.WARNING, + "Failed to register the integration " + + integration.getClass().getName() + + ", a dependency it needs is missing or incompatible", + e); } catch (Throwable t) { + // Integrations can be user-provided, so we can't know what they throw. A failing + // integration is skipped and init continues. + ExceptionUtils.rethrowIfFatal(t); options .getLogger() .log( diff --git a/sentry/src/main/java/io/sentry/SentryClient.java b/sentry/src/main/java/io/sentry/SentryClient.java index b5027432625..ba279517584 100644 --- a/sentry/src/main/java/io/sentry/SentryClient.java +++ b/sentry/src/main/java/io/sentry/SentryClient.java @@ -20,6 +20,7 @@ import io.sentry.transport.ITransport; import io.sentry.transport.RateLimiter; import io.sentry.util.*; +import io.sentry.util.ExceptionUtils; import java.io.Closeable; import java.io.IOException; import java.util.ArrayList; @@ -494,6 +495,13 @@ private SentryEvent processEvent( event = processor.process(event, hint); } } catch (Throwable e) { + // Event processors come from integrations and the app, so we can't know what they throw. + // An SDK processor is skipped; any other processor's failure drops the event. + // A LinkageError means the processor needs an optional dependency that is missing or has an + // incompatible version, which is handled like any other failure. + if (!(e instanceof LinkageError)) { + ExceptionUtils.rethrowIfFatal(e); + } options .getLogger() .log( @@ -551,6 +559,13 @@ private SentryLogEvent processLogEvent( try { event = processor.process(event); } catch (Throwable e) { + // Event processors come from integrations and the app, so we can't know what they throw. + // An SDK processor is skipped; any other processor's failure drops the log. + // A LinkageError means the processor needs an optional dependency that is missing or has an + // incompatible version, which is handled like any other failure. + if (!(e instanceof LinkageError)) { + ExceptionUtils.rethrowIfFatal(e); + } options .getLogger() .log( @@ -588,6 +603,13 @@ private SentryMetricsEvent processMetricsEvent( try { event = processor.process(event, hint); } catch (Throwable e) { + // Event processors come from integrations and the app, so we can't know what they throw. + // An SDK processor is skipped; any other processor's failure drops the metric. + // A LinkageError means the processor needs an optional dependency that is missing or has an + // incompatible version, which is handled like any other failure. + if (!(e instanceof LinkageError)) { + ExceptionUtils.rethrowIfFatal(e); + } options .getLogger() .log( @@ -625,6 +647,13 @@ private SentryMetricsEvent processMetricsEvent( try { transaction = processor.process(transaction, hint); } catch (Throwable e) { + // Event processors come from integrations and the app, so we can't know what they throw. + // An SDK processor is skipped; any other processor's failure drops the transaction. + // A LinkageError means the processor needs an optional dependency that is missing or has an + // incompatible version, which is handled like any other failure. + if (!(e instanceof LinkageError)) { + ExceptionUtils.rethrowIfFatal(e); + } options .getLogger() .log( @@ -698,6 +727,13 @@ private SentryReplayEvent processReplayEvent( try { replayEvent = processor.process(replayEvent, hint); } catch (Throwable e) { + // Event processors come from integrations and the app, so we can't know what they throw. + // An SDK processor is skipped; any other processor's failure drops the replay segment. + // A LinkageError means the processor needs an optional dependency that is missing or has an + // incompatible version, which is handled like any other failure. + if (!(e instanceof LinkageError)) { + ExceptionUtils.rethrowIfFatal(e); + } options .getLogger() .log( @@ -738,6 +774,13 @@ private SentryEvent processFeedbackEvent( try { feedbackEvent = processor.process(feedbackEvent, hint); } catch (Throwable e) { + // Event processors come from integrations and the app, so we can't know what they throw. + // An SDK processor is skipped; any other processor's failure drops the feedback. + // A LinkageError means the processor needs an optional dependency that is missing or has an + // incompatible version, which is handled like any other failure. + if (!(e instanceof LinkageError)) { + ExceptionUtils.rethrowIfFatal(e); + } options .getLogger() .log( diff --git a/sentry/src/main/java/io/sentry/SentryOptions.java b/sentry/src/main/java/io/sentry/SentryOptions.java index 5ba97164a5e..60b1f3cd426 100644 --- a/sentry/src/main/java/io/sentry/SentryOptions.java +++ b/sentry/src/main/java/io/sentry/SentryOptions.java @@ -744,8 +744,8 @@ public void activate() { try { final Class clazz = Class.forName("io.sentry.spotlight.SpotlightIntegration"); integrations.add((Integration) clazz.getConstructor().newInstance()); - } catch (Throwable ignored) { - // SpotlightIntegration not available + } catch (ReflectiveOperationException | LinkageError ignored) { + // sentry-spotlight is missing or has an incompatible version } } } diff --git a/sentry/src/main/java/io/sentry/profiling/ProfilingServiceLoader.java b/sentry/src/main/java/io/sentry/profiling/ProfilingServiceLoader.java index cf86a13fc53..683eea8a866 100644 --- a/sentry/src/main/java/io/sentry/profiling/ProfilingServiceLoader.java +++ b/sentry/src/main/java/io/sentry/profiling/ProfilingServiceLoader.java @@ -9,6 +9,7 @@ import io.sentry.ScopesAdapter; import io.sentry.SentryLevel; import java.util.Iterator; +import java.util.ServiceConfigurationError; import java.util.ServiceLoader; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -38,7 +39,8 @@ public final class ProfilingServiceLoader { logger.log( SentryLevel.DEBUG, "No continuous profiler provider found, using NoOpContinuousProfiler"); return NoOpContinuousProfiler.getInstance(); - } catch (Throwable t) { + } catch (ServiceConfigurationError | LinkageError t) { + // The provider is broken, or async-profiler's native library failed to load logger.log( SentryLevel.ERROR, "Failed to load continuous profiler provider, using NoOpContinuousProfiler", @@ -68,7 +70,8 @@ public final class ProfilingServiceLoader { SentryLevel.DEBUG, "No profile converter provider found, using NoOpProfileConverter"); return NoOpProfileConverter.getInstance(); } - } catch (Throwable t) { + } catch (ServiceConfigurationError | LinkageError t) { + // The provider is broken, or a class it needs is missing logger.log( SentryLevel.ERROR, "Failed to load profile converter provider, using NoOpProfileConverter", diff --git a/sentry/src/main/java/io/sentry/util/LoadClass.java b/sentry/src/main/java/io/sentry/util/LoadClass.java index bf179fad459..aedd27a14be 100644 --- a/sentry/src/main/java/io/sentry/util/LoadClass.java +++ b/sentry/src/main/java/io/sentry/util/LoadClass.java @@ -41,7 +41,8 @@ public class LoadClass { if (logger != null) { logger.log(SentryLevel.ERROR, "Failed to load (UnsatisfiedLinkError) " + clazz, e); } - } catch (Throwable e) { + } catch (LinkageError e) { + // e.g. ExceptionInInitializerError, or NoClassDefFoundError for a dependency of the class if (logger != null) { logger.log(SentryLevel.ERROR, "Failed to initialize " + clazz, e); }