From aeb148910fbfab6112c542961c8d4aa701fb2a19 Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Fri, 22 Jan 2021 09:32:25 +0100 Subject: [PATCH] Add convenient method for accessing event's throwable. Fixes #1201 --- sentry/api/sentry.api | 2 ++ .../main/java/io/sentry/SentryBaseEvent.java | 16 ++++++++++++++++ .../src/test/java/io/sentry/SentryEventTest.kt | 17 +++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 89a87f360a..3ae6cde0cd 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -515,6 +515,7 @@ public final class io/sentry/Sentry { public static fun init (Lio/sentry/Sentry$OptionsConfiguration;)V public static fun init (Lio/sentry/Sentry$OptionsConfiguration;Z)V public static fun init (Lio/sentry/SentryOptions;)V + public static fun init (Ljava/lang/String;)V public static fun isEnabled ()Z public static fun popScope ()V public static fun pushScope ()V @@ -546,6 +547,7 @@ public abstract class io/sentry/SentryBaseEvent { public fun getContexts ()Lio/sentry/protocol/Contexts; public fun getEnvironment ()Ljava/lang/String; public fun getEventId ()Lio/sentry/protocol/SentryId; + public fun getOriginThrowable ()Ljava/lang/Throwable; public fun getRelease ()Ljava/lang/String; public fun getRequest ()Lio/sentry/protocol/Request; public fun getSdk ()Lio/sentry/protocol/SdkVersion; diff --git a/sentry/src/main/java/io/sentry/SentryBaseEvent.java b/sentry/src/main/java/io/sentry/SentryBaseEvent.java index 277e114b7a..096219b9b3 100644 --- a/sentry/src/main/java/io/sentry/SentryBaseEvent.java +++ b/sentry/src/main/java/io/sentry/SentryBaseEvent.java @@ -1,5 +1,6 @@ package io.sentry; +import io.sentry.exception.ExceptionMechanismException; import io.sentry.protocol.Contexts; import io.sentry.protocol.Request; import io.sentry.protocol.SdkVersion; @@ -106,6 +107,21 @@ public void setRequest(final @Nullable Request request) { return throwable; } + /** + * Returns the captured Throwable or null. If a throwable is wrapped in {@link + * ExceptionMechanismException}, returns unwrapped throwable. + * + * @return the Throwable or null + */ + public @Nullable Throwable getOriginThrowable() { + final Throwable ex = throwable; + if (ex instanceof ExceptionMechanismException) { + return ((ExceptionMechanismException) ex).getThrowable(); + } else { + return ex; + } + } + /** * Sets the Throwable * diff --git a/sentry/src/test/java/io/sentry/SentryEventTest.kt b/sentry/src/test/java/io/sentry/SentryEventTest.kt index 9214d0f62c..878be32c7f 100644 --- a/sentry/src/test/java/io/sentry/SentryEventTest.kt +++ b/sentry/src/test/java/io/sentry/SentryEventTest.kt @@ -67,9 +67,26 @@ class SentryEventTest { assertFalse(event.isCrashed) } + @Test fun `adds breadcrumb with string as a parameter`() { val event = SentryEvent() event.addBreadcrumb("breadcrumb") assertEquals(1, event.breadcrumbs.filter { it.message == "breadcrumb" }.size) } + + @Test + fun `when throwable is a ExceptionMechanismException, getOriginThrowable unwraps original throwable`() { + val event = SentryEvent() + val ex = RuntimeException() + event.throwable = ExceptionMechanismException(null, ex, null) + assertEquals(ex, event.originThrowable) + } + + @Test + fun `when throwable is not a ExceptionMechanismException, getOriginThrowable returns throwable`() { + val event = SentryEvent() + val ex = RuntimeException() + event.throwable = ex + assertEquals(ex, event.originThrowable) + } }