Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenient method for accessing event's throwable. #1202

Merged
merged 4 commits into from
Jan 22, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* Enhancement: Enable Kotlin map-like access on CustomSamplingContext (#1192)
* Enhancement: Auto register custom ITransportFactory in Spring integration (#1194)
* Enhancement: Improve Kotlin property access in Performance API (#1193)
* Enhancement: Add convenient method for accessing event's throwable (1202)

# 4.0.0-alpha.3

Expand Down
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.sentry;

import io.sentry.exception.ExceptionMechanismException;
import io.sentry.util.Objects;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -20,34 +19,19 @@ public DuplicateEventDetectionEventProcessor(final @NotNull SentryOptions option

@Override
public SentryEvent process(final @NotNull SentryEvent event, final @Nullable Object hint) {
final Throwable throwable = event.getThrowable();
final Throwable throwable = event.getOriginThrowable();
if (throwable != null) {
if (throwable instanceof ExceptionMechanismException) {
final ExceptionMechanismException ex = (ExceptionMechanismException) throwable;
if (capturedObjects.containsKey(ex.getThrowable())) {
options
.getLogger()
.log(
SentryLevel.DEBUG,
"Duplicate Exception detected. Event %s will be discarded.",
event.getEventId());
return null;
} else {
capturedObjects.put(ex.getThrowable(), null);
}
if (capturedObjects.containsKey(throwable)
|| containsAnyKey(capturedObjects, allCauses(throwable))) {
options
.getLogger()
.log(
SentryLevel.DEBUG,
"Duplicate Exception detected. Event %s will be discarded.",
event.getEventId());
return null;
} else {
if (capturedObjects.containsKey(throwable)
|| containsAnyKey(capturedObjects, allCauses(throwable))) {
options
.getLogger()
.log(
SentryLevel.DEBUG,
"Duplicate Exception detected. Event %s will be discarded.",
event.getEventId());
return null;
} else {
capturedObjects.put(throwable, null);
}
capturedObjects.put(throwable, null);
}
}
return event;
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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could remove the code dup from DuplicateEventDetectionEventProcessor using this method now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True! Will be removed in few mins.

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)
}
}