Repository navigation
Expand file tree
/
Copy pathSentryCaptureExceptionParameterAdvice.java
More file actions
66 lines (58 loc) · 2.37 KB
/
Copy pathSentryCaptureExceptionParameterAdvice.java
File metadata and controls
66 lines (58 loc) · 2.37 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
package io.sentry.spring7.exception;
import com.jakewharton.nopen.annotation.Open;
import io.sentry.IScopes;
import io.sentry.ScopesAdapter;
import io.sentry.exception.ExceptionMechanismException;
import io.sentry.protocol.Mechanism;
import io.sentry.util.Objects;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.aop.support.AopUtils;
import org.springframework.core.annotation.AnnotationUtils;
/**
* Captures an exception passed to a bean method annotated with {@link
* SentryCaptureExceptionParameter}.
*/
@ApiStatus.Internal
@Open
public class SentryCaptureExceptionParameterAdvice implements MethodInterceptor {
private static final String MECHANISM_TYPE = "SentrySpring7CaptureExceptionParameterAdvice";
private final @NotNull IScopes scopes;
public SentryCaptureExceptionParameterAdvice() {
this(ScopesAdapter.getInstance());
}
public SentryCaptureExceptionParameterAdvice(final @NotNull IScopes scopes) {
this.scopes = Objects.requireNonNull(scopes, "scopes are required");
}
@Override
public @Nullable Object invoke(final @NotNull MethodInvocation invocation) throws Throwable {
final @Nullable Object target = invocation.getThis();
final Method mostSpecificMethod =
AopUtils.getMostSpecificMethod(
invocation.getMethod(), target != null ? target.getClass() : null);
SentryCaptureExceptionParameter sentryCaptureExceptionParameter =
AnnotationUtils.findAnnotation(mostSpecificMethod, SentryCaptureExceptionParameter.class);
if (sentryCaptureExceptionParameter != null) {
Object[] args = invocation.getArguments();
for (Object arg : args) {
if (arg instanceof Exception exception) {
captureException(exception);
break;
}
}
}
return invocation.proceed();
}
private void captureException(final @NotNull Throwable throwable) {
final Mechanism mechanism = new Mechanism();
mechanism.setType(MECHANISM_TYPE);
mechanism.setHandled(true);
final Throwable mechanismException =
new ExceptionMechanismException(mechanism, throwable, Thread.currentThread());
scopes.captureException(mechanismException);
}
}