Skip to content

Commit 3eaa67f

Browse files
Add convenient method for accessing event's throwable. (#1202)
Fixes #1201
1 parent db14f63 commit 3eaa67f

File tree

5 files changed

+46
-27
lines changed

5 files changed

+46
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Enhancement: Enable Kotlin map-like access on CustomSamplingContext (#1192)
1717
* Enhancement: Auto register custom ITransportFactory in Spring integration (#1194)
1818
* Enhancement: Improve Kotlin property access in Performance API (#1193)
19+
* Enhancement: Add convenient method for accessing event's throwable (1202)
1920

2021
# 4.0.0-alpha.3
2122

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ public abstract class io/sentry/SentryBaseEvent {
544544
public fun getContexts ()Lio/sentry/protocol/Contexts;
545545
public fun getEnvironment ()Ljava/lang/String;
546546
public fun getEventId ()Lio/sentry/protocol/SentryId;
547+
public fun getOriginThrowable ()Ljava/lang/Throwable;
547548
public fun getRelease ()Ljava/lang/String;
548549
public fun getRequest ()Lio/sentry/protocol/Request;
549550
public fun getSdk ()Lio/sentry/protocol/SdkVersion;

sentry/src/main/java/io/sentry/DuplicateEventDetectionEventProcessor.java

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.sentry;
22

3-
import io.sentry.exception.ExceptionMechanismException;
43
import io.sentry.util.Objects;
54
import java.util.ArrayList;
65
import java.util.List;
@@ -20,34 +19,19 @@ public DuplicateEventDetectionEventProcessor(final @NotNull SentryOptions option
2019

2120
@Override
2221
public SentryEvent process(final @NotNull SentryEvent event, final @Nullable Object hint) {
23-
final Throwable throwable = event.getThrowable();
22+
final Throwable throwable = event.getOriginThrowable();
2423
if (throwable != null) {
25-
if (throwable instanceof ExceptionMechanismException) {
26-
final ExceptionMechanismException ex = (ExceptionMechanismException) throwable;
27-
if (capturedObjects.containsKey(ex.getThrowable())) {
28-
options
29-
.getLogger()
30-
.log(
31-
SentryLevel.DEBUG,
32-
"Duplicate Exception detected. Event %s will be discarded.",
33-
event.getEventId());
34-
return null;
35-
} else {
36-
capturedObjects.put(ex.getThrowable(), null);
37-
}
24+
if (capturedObjects.containsKey(throwable)
25+
|| containsAnyKey(capturedObjects, allCauses(throwable))) {
26+
options
27+
.getLogger()
28+
.log(
29+
SentryLevel.DEBUG,
30+
"Duplicate Exception detected. Event %s will be discarded.",
31+
event.getEventId());
32+
return null;
3833
} else {
39-
if (capturedObjects.containsKey(throwable)
40-
|| containsAnyKey(capturedObjects, allCauses(throwable))) {
41-
options
42-
.getLogger()
43-
.log(
44-
SentryLevel.DEBUG,
45-
"Duplicate Exception detected. Event %s will be discarded.",
46-
event.getEventId());
47-
return null;
48-
} else {
49-
capturedObjects.put(throwable, null);
50-
}
34+
capturedObjects.put(throwable, null);
5135
}
5236
}
5337
return event;

sentry/src/main/java/io/sentry/SentryBaseEvent.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry;
22

3+
import io.sentry.exception.ExceptionMechanismException;
34
import io.sentry.protocol.Contexts;
45
import io.sentry.protocol.Request;
56
import io.sentry.protocol.SdkVersion;
@@ -106,6 +107,21 @@ public void setRequest(final @Nullable Request request) {
106107
return throwable;
107108
}
108109

110+
/**
111+
* Returns the captured Throwable or null. If a throwable is wrapped in {@link
112+
* ExceptionMechanismException}, returns unwrapped throwable.
113+
*
114+
* @return the Throwable or null
115+
*/
116+
public @Nullable Throwable getOriginThrowable() {
117+
final Throwable ex = throwable;
118+
if (ex instanceof ExceptionMechanismException) {
119+
return ((ExceptionMechanismException) ex).getThrowable();
120+
} else {
121+
return ex;
122+
}
123+
}
124+
109125
/**
110126
* Sets the Throwable
111127
*

sentry/src/test/java/io/sentry/SentryEventTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,26 @@ class SentryEventTest {
6767
assertFalse(event.isCrashed)
6868
}
6969

70+
@Test
7071
fun `adds breadcrumb with string as a parameter`() {
7172
val event = SentryEvent()
7273
event.addBreadcrumb("breadcrumb")
7374
assertEquals(1, event.breadcrumbs.filter { it.message == "breadcrumb" }.size)
7475
}
76+
77+
@Test
78+
fun `when throwable is a ExceptionMechanismException, getOriginThrowable unwraps original throwable`() {
79+
val event = SentryEvent()
80+
val ex = RuntimeException()
81+
event.throwable = ExceptionMechanismException(null, ex, null)
82+
assertEquals(ex, event.originThrowable)
83+
}
84+
85+
@Test
86+
fun `when throwable is not a ExceptionMechanismException, getOriginThrowable returns throwable`() {
87+
val event = SentryEvent()
88+
val ex = RuntimeException()
89+
event.throwable = ex
90+
assertEquals(ex, event.originThrowable)
91+
}
7592
}

0 commit comments

Comments
 (0)