-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
UncaughtExceptionHandlerIntegration.java
181 lines (155 loc) · 6 KB
/
UncaughtExceptionHandlerIntegration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package io.sentry;
import static io.sentry.SentryLevel.ERROR;
import io.sentry.exception.ExceptionMechanismException;
import io.sentry.hints.DiskFlushNotification;
import io.sentry.hints.Flushable;
import io.sentry.hints.SessionEnd;
import io.sentry.protocol.Mechanism;
import io.sentry.protocol.SentryId;
import io.sentry.util.HintUtils;
import io.sentry.util.Objects;
import java.io.Closeable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
/**
* Sends any uncaught exception to Sentry, then passes the exception on to the pre-existing uncaught
* exception handler.
*/
public final class UncaughtExceptionHandlerIntegration
implements Integration, Thread.UncaughtExceptionHandler, Closeable {
/** Reference to the pre-existing uncaught exception handler. */
private @Nullable Thread.UncaughtExceptionHandler defaultExceptionHandler;
private @Nullable IHub hub;
private @Nullable SentryOptions options;
private boolean registered = false;
private final @NotNull UncaughtExceptionHandler threadAdapter;
public UncaughtExceptionHandlerIntegration() {
this(UncaughtExceptionHandler.Adapter.getInstance());
}
UncaughtExceptionHandlerIntegration(final @NotNull UncaughtExceptionHandler threadAdapter) {
this.threadAdapter = Objects.requireNonNull(threadAdapter, "threadAdapter is required.");
}
@Override
public final void register(final @NotNull IHub hub, final @NotNull SentryOptions options) {
if (registered) {
options
.getLogger()
.log(
SentryLevel.ERROR,
"Attempt to register a UncaughtExceptionHandlerIntegration twice.");
return;
}
registered = true;
this.hub = Objects.requireNonNull(hub, "Hub is required");
this.options = Objects.requireNonNull(options, "SentryOptions is required");
this.options
.getLogger()
.log(
SentryLevel.DEBUG,
"UncaughtExceptionHandlerIntegration enabled: %s",
this.options.isEnableUncaughtExceptionHandler());
if (this.options.isEnableUncaughtExceptionHandler()) {
final Thread.UncaughtExceptionHandler currentHandler =
threadAdapter.getDefaultUncaughtExceptionHandler();
if (currentHandler != null) {
this.options
.getLogger()
.log(
SentryLevel.DEBUG,
"default UncaughtExceptionHandler class='"
+ currentHandler.getClass().getName()
+ "'");
defaultExceptionHandler = currentHandler;
}
threadAdapter.setDefaultUncaughtExceptionHandler(this);
this.options
.getLogger()
.log(SentryLevel.DEBUG, "UncaughtExceptionHandlerIntegration installed.");
}
}
@Override
public void uncaughtException(Thread thread, Throwable thrown) {
if (options != null && hub != null) {
options.getLogger().log(SentryLevel.INFO, "Uncaught exception received.");
try {
final UncaughtExceptionHint exceptionHint =
new UncaughtExceptionHint(options.getFlushTimeoutMillis(), options.getLogger());
final Throwable throwable = getUnhandledThrowable(thread, thrown);
final SentryEvent event = new SentryEvent(throwable);
event.setLevel(SentryLevel.FATAL);
final Hint hint = HintUtils.createWithTypeCheckHint(exceptionHint);
final @NotNull SentryId sentryId = hub.captureEvent(event, hint);
final boolean isEventDropped = sentryId.equals(SentryId.EMPTY_ID);
if (!isEventDropped) {
// Block until the event is flushed to disk
if (!exceptionHint.waitFlush()) {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Timed out waiting to flush event to disk before crashing. Event: %s",
event.getEventId());
}
}
} catch (Throwable e) {
options
.getLogger()
.log(SentryLevel.ERROR, "Error sending uncaught exception to Sentry.", e);
}
if (defaultExceptionHandler != null) {
options.getLogger().log(SentryLevel.INFO, "Invoking inner uncaught exception handler.");
defaultExceptionHandler.uncaughtException(thread, thrown);
} else {
if (options.isPrintUncaughtStackTrace()) {
thrown.printStackTrace();
}
}
}
}
@TestOnly
@NotNull
static Throwable getUnhandledThrowable(
final @NotNull Thread thread, final @NotNull Throwable thrown) {
final Mechanism mechanism = new Mechanism();
mechanism.setHandled(false);
mechanism.setType("UncaughtExceptionHandler");
return new ExceptionMechanismException(mechanism, thrown, thread);
}
@Override
public void close() {
if (this == threadAdapter.getDefaultUncaughtExceptionHandler()) {
threadAdapter.setDefaultUncaughtExceptionHandler(defaultExceptionHandler);
if (options != null) {
options.getLogger().log(SentryLevel.DEBUG, "UncaughtExceptionHandlerIntegration removed.");
}
}
}
private static final class UncaughtExceptionHint
implements DiskFlushNotification, Flushable, SessionEnd {
private final CountDownLatch latch;
private final long flushTimeoutMillis;
private final @NotNull ILogger logger;
UncaughtExceptionHint(final long flushTimeoutMillis, final @NotNull ILogger logger) {
this.flushTimeoutMillis = flushTimeoutMillis;
latch = new CountDownLatch(1);
this.logger = logger;
}
@Override
public boolean waitFlush() {
try {
return latch.await(flushTimeoutMillis, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.log(ERROR, "Exception while awaiting for flush in UncaughtExceptionHint", e);
}
return false;
}
@Override
public void markFlushed() {
latch.countDown();
}
}
}