Repository navigation
Expand file tree
/
Copy pathSentryExceptionResolver.java
More file actions
95 lines (79 loc) · 3.26 KB
/
Copy pathSentryExceptionResolver.java
File metadata and controls
95 lines (79 loc) · 3.26 KB
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
package io.sentry.spring7;
import static io.sentry.TypeCheckHint.SPRING_RESOLVER_REQUEST;
import static io.sentry.TypeCheckHint.SPRING_RESOLVER_RESPONSE;
import com.jakewharton.nopen.annotation.Open;
import io.sentry.Hint;
import io.sentry.IScopes;
import io.sentry.SentryEvent;
import io.sentry.SentryLevel;
import io.sentry.exception.ExceptionMechanismException;
import io.sentry.protocol.Mechanism;
import io.sentry.spring7.tracing.TransactionNameProvider;
import io.sentry.util.Objects;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
/**
* {@link HandlerExceptionResolver} implementation that will record any exception that a Spring
* {@link org.springframework.web.servlet.mvc.Controller} throws to Sentry. It then returns null,
* which will let the other (default or custom) exception resolvers handle the actual error.
*/
@Open
public class SentryExceptionResolver implements HandlerExceptionResolver, Ordered {
public static final String MECHANISM_TYPE = "Spring7ExceptionResolver";
private final @NotNull IScopes scopes;
private final @NotNull TransactionNameProvider transactionNameProvider;
private final int order;
public SentryExceptionResolver(
final @NotNull IScopes scopes,
final @NotNull TransactionNameProvider transactionNameProvider,
final int order) {
this.scopes = Objects.requireNonNull(scopes, "scopes are required");
this.transactionNameProvider =
Objects.requireNonNull(transactionNameProvider, "transactionNameProvider is required");
this.order = order;
}
@Override
public @Nullable ModelAndView resolveException(
final @NotNull HttpServletRequest request,
final @NotNull HttpServletResponse response,
final @Nullable Object handler,
final @NotNull Exception ex) {
final SentryEvent event = createEvent(request, ex);
final Hint hint = createHint(request, response);
scopes.captureEvent(event, hint);
// null = run other HandlerExceptionResolvers to actually handle the exception
return null;
}
@Override
public int getOrder() {
return order;
}
@NotNull
protected SentryEvent createEvent(
final @NotNull HttpServletRequest request, final @NotNull Exception ex) {
final Mechanism mechanism = new Mechanism();
mechanism.setHandled(false);
mechanism.setType(MECHANISM_TYPE);
final Throwable throwable =
new ExceptionMechanismException(mechanism, ex, Thread.currentThread());
final SentryEvent event = new SentryEvent(throwable);
event.setLevel(SentryLevel.FATAL);
event.setTransaction(
TransactionNameProviderUtils.provideTransactionName(
transactionNameProvider, request, scopes.getOptions().getLogger()));
return event;
}
@Nullable
protected Hint createHint(
final @NotNull HttpServletRequest request, final @NotNull HttpServletResponse response) {
final Hint hint = new Hint();
hint.set(SPRING_RESOLVER_REQUEST, request);
hint.set(SPRING_RESOLVER_RESPONSE, response);
return hint;
}
}