Repository navigation
Expand file tree
/
Copy pathSentryWebExceptionHandler.java
More file actions
73 lines (64 loc) · 3.15 KB
/
Copy pathSentryWebExceptionHandler.java
File metadata and controls
73 lines (64 loc) · 3.15 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
package io.sentry.spring7.webflux;
import static io.sentry.TypeCheckHint.WEBFLUX_EXCEPTION_HANDLER_EXCHANGE;
import static io.sentry.TypeCheckHint.WEBFLUX_EXCEPTION_HANDLER_REQUEST;
import static io.sentry.TypeCheckHint.WEBFLUX_EXCEPTION_HANDLER_RESPONSE;
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.reactor.SentryReactorUtils;
import io.sentry.util.Objects;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.core.annotation.Order;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Mono;
/** Handles unhandled exceptions in Spring WebFlux integration. */
@Order(
-2) // the DefaultErrorWebExceptionHandler provided by Spring Boot for error handling is ordered
// at -1
@ApiStatus.Experimental
public final class SentryWebExceptionHandler implements WebExceptionHandler {
public static final String MECHANISM_TYPE = "Spring7WebFluxExceptionResolver";
private final @NotNull IScopes scopes;
public SentryWebExceptionHandler(final @NotNull IScopes scopes) {
this.scopes = Objects.requireNonNull(scopes, "scopes are required");
}
@Override
public @NotNull Mono<Void> handle(
final @NotNull ServerWebExchange serverWebExchange, final @NotNull Throwable ex) {
final @Nullable IScopes requestScopes =
serverWebExchange.getAttribute(SentryWebFilter.SENTRY_SCOPES_KEY);
final @NotNull IScopes scopesToUse = requestScopes != null ? requestScopes : scopes;
return SentryReactorUtils.withSentryScopes(
Mono.just(ex)
.map(
it -> {
if (!(ex instanceof ResponseStatusException)) {
final Mechanism mechanism = new Mechanism();
mechanism.setType(MECHANISM_TYPE);
mechanism.setHandled(false);
final Throwable throwable =
new ExceptionMechanismException(mechanism, ex, Thread.currentThread());
final SentryEvent event = new SentryEvent(throwable);
event.setLevel(SentryLevel.FATAL);
event.setTransaction(
TransactionNameProvider.provideTransactionName(serverWebExchange));
final Hint hint = new Hint();
hint.set(WEBFLUX_EXCEPTION_HANDLER_REQUEST, serverWebExchange.getRequest());
hint.set(
WEBFLUX_EXCEPTION_HANDLER_RESPONSE, serverWebExchange.getResponse());
hint.set(WEBFLUX_EXCEPTION_HANDLER_EXCHANGE, serverWebExchange);
scopes.captureEvent(event, hint);
}
return it;
}),
scopesToUse)
.flatMap(it -> Mono.error(ex));
}
}