Skip to content

Commit

Permalink
Add convenient method for accessing event's throwable.
Browse files Browse the repository at this point in the history
Fixes #1201
  • Loading branch information
maciejwalkowiak committed Jan 22, 2021
1 parent d6c2ede commit aeb1489
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions sentry/src/main/java/io/sentry/SentryBaseEvent.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
17 changes: 17 additions & 0 deletions sentry/src/test/java/io/sentry/SentryEventTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit aeb1489

Please sign in to comment.