Skip to content

Commit

Permalink
Add options.printUncaughtStackTrace (#1890)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paxa authored Feb 2, 2022
1 parent 46929b0 commit 50c088c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Fix: NPE while adding "response_body_size" breadcrumb, when response body is null (#1884)
* Bump: AGP to 7.1.0 (#1892)
* Feat: Add options.printUncaughtStackTrace to print uncaught exceptions (#1890)

## 5.6.0

Expand Down
3 changes: 3 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ public class io/sentry/SentryOptions {
public fun getMaxRequestBodySize ()Lio/sentry/SentryOptions$RequestSize;
public fun getMaxSpans ()I
public fun getOutboxPath ()Ljava/lang/String;
public fun getPrintUncaughtStackTrace ()Ljava/lang/Boolean;
public fun getProguardUuid ()Ljava/lang/String;
public fun getProxy ()Lio/sentry/SentryOptions$Proxy;
public fun getReadTimeoutMillis ()I
Expand Down Expand Up @@ -884,6 +885,7 @@ public class io/sentry/SentryOptions {
public fun isEnableSessionTracking ()Z
public fun isEnableShutdownHook ()Z
public fun isEnableUncaughtExceptionHandler ()Z
public fun isPrintUncaughtStackTrace ()Z
public fun isSendDefaultPii ()Z
public fun isTraceSampling ()Z
public fun isTracingEnabled ()Z
Expand Down Expand Up @@ -920,6 +922,7 @@ public class io/sentry/SentryOptions {
public fun setMaxQueueSize (I)V
public fun setMaxRequestBodySize (Lio/sentry/SentryOptions$RequestSize;)V
public fun setMaxSpans (I)V
public fun setPrintUncaughtStackTrace (Ljava/lang/Boolean;)V
public fun setProguardUuid (Ljava/lang/String;)V
public fun setProxy (Lio/sentry/SentryOptions$Proxy;)V
public fun setReadTimeoutMillis (I)V
Expand Down
38 changes: 38 additions & 0 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ public class SentryOptions {
*/
private @Nullable Boolean enableUncaughtExceptionHandler = true;

/*
* When enabled, UncaughtExceptionHandler will print exceptions (same as java would normally do),
* if no other UncaughtExceptionHandler was registered before.
*/
private @Nullable Boolean printUncaughtStackTrace = false;

/** Sentry Executor Service that sends cached events and envelopes on App. start. */
private @NotNull ISentryExecutorService executorService = NoOpSentryExecutorService.getInstance();

Expand Down Expand Up @@ -309,6 +315,8 @@ public class SentryOptions {
options.setServerName(propertiesProvider.getProperty("servername"));
options.setEnableUncaughtExceptionHandler(
propertiesProvider.getBooleanProperty("uncaught.handler.enabled"));
options.setPrintUncaughtStackTrace(
propertiesProvider.getBooleanProperty("uncaught.handler.print-stacktrace"));
options.setTracesSampleRate(propertiesProvider.getDoubleProperty("traces-sample-rate"));
options.setDebug(propertiesProvider.getBooleanProperty("debug"));
options.setEnableDeduplication(propertiesProvider.getBooleanProperty("enable-deduplication"));
Expand Down Expand Up @@ -1070,6 +1078,33 @@ public void setEnableUncaughtExceptionHandler(
this.enableUncaughtExceptionHandler = enableUncaughtExceptionHandler;
}

/**
* Checks if printing exceptions by UncaughtExceptionHandler is enabled or disabled.
*
* @return true if enabled or false otherwise.
*/
public boolean isPrintUncaughtStackTrace() {
return Boolean.TRUE.equals(printUncaughtStackTrace);
}

/**
* Checks if printing exceptions by UncaughtExceptionHandler is enabled or disabled.
*
* @return true if enabled, false otherwise or null if not set.
*/
public @Nullable Boolean getPrintUncaughtStackTrace() {
return printUncaughtStackTrace;
}

/**
* Enable or disable printing exceptions in UncaughtExceptionHandler
*
* @param printUncaughtStackTrace true if enabled or false otherwise.
*/
public void setPrintUncaughtStackTrace(final @Nullable Boolean printUncaughtStackTrace) {
this.printUncaughtStackTrace = printUncaughtStackTrace;
}

/**
* Returns the SentryExecutorService
*
Expand Down Expand Up @@ -1622,6 +1657,9 @@ void merge(final @NotNull SentryOptions options) {
if (options.getEnableUncaughtExceptionHandler() != null) {
setEnableUncaughtExceptionHandler(options.getEnableUncaughtExceptionHandler());
}
if (options.getPrintUncaughtStackTrace() != null) {
setPrintUncaughtStackTrace(options.getPrintUncaughtStackTrace());
}
if (options.getTracesSampleRate() != null) {
setTracesSampleRate(options.getTracesSampleRate());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public void uncaughtException(Thread thread, Throwable thrown) {
if (defaultExceptionHandler != null) {
options.getLogger().log(SentryLevel.INFO, "Invoking inner uncaught exception handler.");
defaultExceptionHandler.uncaughtException(thread, thrown);
} else {
if (options.isPrintUncaughtStackTrace()) {
thrown.printStackTrace();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import com.nhaarman.mockitokotlin2.whenever
import io.sentry.exception.ExceptionMechanismException
import io.sentry.protocol.SentryId
import io.sentry.util.noFlushTimeout
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.PrintStream
import java.nio.file.Files
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
Expand Down Expand Up @@ -150,4 +152,31 @@ class UncaughtExceptionHandlerIntegrationTest {
integration.close()
verify(handlerMock).defaultUncaughtExceptionHandler = null
}

@Test
fun `When printUncaughtStackTrace is enabled, prints the stacktrace to standard error`() {
val standardErr = System.err
try {
val outputStreamCaptor = ByteArrayOutputStream()
System.setErr(PrintStream(outputStreamCaptor))

val handlerMock = mock<UncaughtExceptionHandler>()
val options = SentryOptions().noFlushTimeout()
options.printUncaughtStackTrace = true
val sut = UncaughtExceptionHandlerIntegration(handlerMock)
sut.register(mock<IHub>(), options)
sut.uncaughtException(mock<Thread>(), RuntimeException("This should be printed!"))

assertTrue(
outputStreamCaptor.toString()
.contains("java.lang.RuntimeException: This should be printed!")
)
assertTrue(
outputStreamCaptor.toString()
.contains("UncaughtExceptionHandlerIntegrationTest.kt:")
)
} finally {
System.setErr(standardErr)
}
}
}

0 comments on commit 50c088c

Please sign in to comment.