Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions sentry/src/main/java/io/sentry/IScopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>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);
Expand All @@ -364,13 +370,24 @@ default void addBreadcrumb(@NotNull String message, @NotNull String category) {
* changes may be dropped when executed in parallel. Use {@link IScopes#configureScope(ScopeType,
* ScopeCallback)} instead.
*
* <p>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);

/**
* Configures the scope through the callback.
*
* <p>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) {
Expand All @@ -380,6 +397,11 @@ default void configureScope(@NotNull ScopeCallback callback) {
/**
* Configures the scope through the callback.
*
* <p>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);
Expand Down
22 changes: 22 additions & 0 deletions sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,12 @@ public static void removeExtra(final @Nullable String key) {
* <p>Unlike {@link Sentry#pushScope()} this also forks in global hub mode, since the previous
* scopes are always restored once the callback returns.
*
* <p>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) {
Expand All @@ -1063,6 +1069,12 @@ public static void withScope(final @NotNull ScopeCallback callback) {
* <p>Unlike {@link Sentry#pushIsolationScope()} this also forks in global hub mode, since the
* previous scopes are always restored once the callback returns.
*
* <p>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) {
Expand All @@ -1072,6 +1084,11 @@ public static void withIsolationScope(final @NotNull ScopeCallback callback) {
/**
* Configures the scope through the callback.
*
* <p>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) {
Expand All @@ -1081,6 +1098,11 @@ public static void configureScope(final @NotNull ScopeCallback callback) {
/**
* Configures the scope through the callback.
*
* <p>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(
Expand Down
73 changes: 73 additions & 0 deletions sentry/src/test/java/io/sentry/ScopesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<OutOfMemoryError> { scopes.withScope { throw OutOfMemoryError("callback") } }
}

@Test
fun `when withScope on a closed scopes throws a fatal error then it propagates`() {
val (scopes) = getEnabledScopes()
scopes.close()

assertFailsWith<OutOfMemoryError> { scopes.withScope { throw OutOfMemoryError("callback") } }
}

// endregion

// region withIsolationScope tests
Expand Down Expand Up @@ -992,6 +1017,35 @@ class ScopesTest {
verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception))
}

@Test
fun `when withIsolationScope throws a runtime exception then it is swallowed`() {
val (scopes, _, logger) = getEnabledScopes()
val exception = IllegalStateException("scope callback exception")

scopes.withIsolationScope { throw exception }

verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception))
}

@Test
fun `when withIsolationScope throws a fatal error then it propagates`() {
val (scopes) = getEnabledScopes()

assertFailsWith<OutOfMemoryError> {
scopes.withIsolationScope { throw OutOfMemoryError("callback") }
}
}

@Test
fun `when withIsolationScope on a closed scopes throws a fatal error then it propagates`() {
val (scopes) = getEnabledScopes()
scopes.close()

assertFailsWith<OutOfMemoryError> {
scopes.withIsolationScope { throw OutOfMemoryError("callback") }
}
}

// endregion

// region configureScope tests
Expand Down Expand Up @@ -1028,6 +1082,25 @@ class ScopesTest {
verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception))
}

@Test
fun `when configureScope throws a runtime exception then it is swallowed`() {
val (scopes, _, logger) = getEnabledScopes()
val exception = IllegalStateException("scope callback exception")

scopes.configureScope { throw exception }

verify(logger).log(eq(SentryLevel.ERROR), any(), eq(exception))
}

@Test
fun `when configureScope throws a fatal error then it propagates`() {
val (scopes) = getEnabledScopes()

assertFailsWith<OutOfMemoryError> {
scopes.configureScope { throw OutOfMemoryError("callback") }
}
}

// endregion

@Test
Expand Down
27 changes: 27 additions & 0 deletions sentry/src/test/java/io/sentry/SentryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFails
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.assertNotEquals
Expand Down Expand Up @@ -1561,6 +1562,32 @@ class SentryTest {
}
}

@Test
fun `scope callbacks that throw a runtime exception are swallowed`() {
initForTest { o -> o.dsn = dsn }

Sentry.withScope { throw IllegalStateException("withScope") }
Sentry.withIsolationScope { throw IllegalStateException("withIsolationScope") }
Sentry.configureScope { throw IllegalStateException("configureScope") }
ScopesAdapter.getInstance().withScope { throw IllegalStateException("adapter withScope") }
}

@Test
fun `scope callbacks that throw a fatal error propagate`() {
initForTest { o -> o.dsn = dsn }

assertFailsWith<OutOfMemoryError> { Sentry.withScope { throw OutOfMemoryError("withScope") } }
assertFailsWith<OutOfMemoryError> {
Sentry.withIsolationScope { throw OutOfMemoryError("withIsolationScope") }
}
assertFailsWith<OutOfMemoryError> {
Sentry.configureScope { throw OutOfMemoryError("configureScope") }
}
assertFailsWith<OutOfMemoryError> {
ScopesAdapter.getInstance().configureScope { throw OutOfMemoryError("adapter") }
}
}

@Test
fun `withScope data does not leak into events captured after the callback`() {
for (globalHubMode in listOf(false, true)) {
Expand Down
Loading