diff --git a/sentry/src/main/java/io/sentry/IScopes.java b/sentry/src/main/java/io/sentry/IScopes.java index 6b5a2b8a5b..c1ffa4f7d6 100644 --- a/sentry/src/main/java/io/sentry/IScopes.java +++ b/sentry/src/main/java/io/sentry/IScopes.java @@ -351,6 +351,12 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) { * changes may be dropped when executed in parallel. Use {@link * IScopes#configureScope(ScopeCallback)} instead. * + *
If the callback throws, the rest of it is skipped and the exception is logged at {@link + * SentryLevel#ERROR} through {@link SentryOptions#getLogger()}, which by default prints only when + * {@link SentryOptions#isDebug()} is enabled. Fatal errors, such as {@link OutOfMemoryError}, are + * rethrown. Before {@link Sentry#init}, the callback runs on a no-op scope and nothing it throws + * is caught. + * * @param callback the callback */ void withScope(@NotNull ScopeCallback callback); @@ -364,6 +370,12 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) { * changes may be dropped when executed in parallel. Use {@link IScopes#configureScope(ScopeType, * ScopeCallback)} instead. * + *
If the callback throws, the rest of it is skipped and the exception is logged at {@link + * SentryLevel#ERROR} through {@link SentryOptions#getLogger()}, which by default prints only when + * {@link SentryOptions#isDebug()} is enabled. Fatal errors, such as {@link OutOfMemoryError}, are + * rethrown. Before {@link Sentry#init}, the callback runs on a no-op scope and nothing it throws + * is caught. + * * @param callback the callback */ void withIsolationScope(@NotNull ScopeCallback callback); @@ -371,6 +383,11 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) { /** * Configures the scope through the callback. * + *
If the callback throws, the rest of it is skipped and the exception is logged at {@link + * SentryLevel#ERROR} through {@link SentryOptions#getLogger()}, which by default prints only when + * {@link SentryOptions#isDebug()} is enabled. Fatal errors, such as {@link OutOfMemoryError}, are + * rethrown. Before {@link Sentry#init}, the callback isn't run. + * * @param callback The configure scope callback. */ default void configureScope(@NotNull ScopeCallback callback) { @@ -380,6 +397,11 @@ default void configureScope(@NotNull ScopeCallback callback) { /** * Configures the scope through the callback. * + *
If the callback throws, the rest of it is skipped and the exception is logged at {@link + * SentryLevel#ERROR} through {@link SentryOptions#getLogger()}, which by default prints only when + * {@link SentryOptions#isDebug()} is enabled. Fatal errors, such as {@link OutOfMemoryError}, are + * rethrown. Before {@link Sentry#init}, the callback isn't run. + * * @param callback The configure scope callback. */ void configureScope(@Nullable ScopeType scopeType, @NotNull ScopeCallback callback); diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index b769b50ddb..bec5ae5d42 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -1050,6 +1050,12 @@ public static void removeExtra(final @Nullable String key) { *
Unlike {@link Sentry#pushScope()} this also forks in global hub mode, since the previous * scopes are always restored once the callback returns. * + *
If the callback throws, the rest of it is skipped and the exception is logged at {@link + * SentryLevel#ERROR} through {@link SentryOptions#getLogger()}, which by default prints only when + * {@link SentryOptions#isDebug()} is enabled. Fatal errors, such as {@link OutOfMemoryError}, are + * rethrown. Before {@link #init} and after {@link #close}, the callback runs on a no-op scope and + * nothing it throws is caught. + * * @param callback the callback */ public static void withScope(final @NotNull ScopeCallback callback) { @@ -1063,6 +1069,12 @@ public static void withScope(final @NotNull ScopeCallback callback) { *
Unlike {@link Sentry#pushIsolationScope()} this also forks in global hub mode, since the * previous scopes are always restored once the callback returns. * + *
If the callback throws, the rest of it is skipped and the exception is logged at {@link + * SentryLevel#ERROR} through {@link SentryOptions#getLogger()}, which by default prints only when + * {@link SentryOptions#isDebug()} is enabled. Fatal errors, such as {@link OutOfMemoryError}, are + * rethrown. Before {@link #init} and after {@link #close}, the callback runs on a no-op scope and + * nothing it throws is caught. + * * @param callback the callback */ public static void withIsolationScope(final @NotNull ScopeCallback callback) { @@ -1072,6 +1084,11 @@ public static void withIsolationScope(final @NotNull ScopeCallback callback) { /** * Configures the scope through the callback. * + *
If the callback throws, the rest of it is skipped and the exception is logged at {@link + * SentryLevel#ERROR} through {@link SentryOptions#getLogger()}, which by default prints only when + * {@link SentryOptions#isDebug()} is enabled. Fatal errors, such as {@link OutOfMemoryError}, are + * rethrown. Before {@link #init} and after {@link #close}, the callback isn't run. + * * @param callback The configure scope callback. */ public static void configureScope(final @NotNull ScopeCallback callback) { @@ -1081,6 +1098,11 @@ public static void configureScope(final @NotNull ScopeCallback callback) { /** * Configures the scope through the callback. * + *
If the callback throws, the rest of it is skipped and the exception is logged at {@link
+ * SentryLevel#ERROR} through {@link SentryOptions#getLogger()}, which by default prints only when
+ * {@link SentryOptions#isDebug()} is enabled. Fatal errors, such as {@link OutOfMemoryError}, are
+ * rethrown. Before {@link #init} and after {@link #close}, the callback isn't run.
+ *
* @param callback The configure scope callback.
*/
public static void configureScope(
diff --git a/sentry/src/test/java/io/sentry/ScopesTest.kt b/sentry/src/test/java/io/sentry/ScopesTest.kt
index acd2b764ad..1bf1f6529f 100644
--- a/sentry/src/test/java/io/sentry/ScopesTest.kt
+++ b/sentry/src/test/java/io/sentry/ScopesTest.kt
@@ -956,6 +956,31 @@ class ScopesTest {
verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception))
}
+ @Test
+ fun `when withScope throws a runtime exception then it is swallowed`() {
+ val (scopes, _, logger) = getEnabledScopes()
+ val exception = IllegalStateException("scope callback exception")
+
+ scopes.withScope { throw exception }
+
+ verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception))
+ }
+
+ @Test
+ fun `when withScope throws a fatal error then it propagates`() {
+ val (scopes) = getEnabledScopes()
+
+ assertFailsWith