-
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Annotation SentryCaptureException for org.springframework.web.bin…
…d.annotation.ExceptionHandler (#2764) Co-authored-by: Alexander Dinauer <alexander.dinauer@sentry.io>
- Loading branch information
Showing
21 changed files
with
662 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...rta/src/main/java/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.sentry.spring.jakarta.exception; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Captures an exception passed to an annotated method. Can be used to capture exceptions from your | ||
* {@link org.springframework.web.bind.annotation.ExceptionHandler} but can also be used on other | ||
* methods. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD}) | ||
public @interface SentryCaptureExceptionParameter {} |
58 changes: 58 additions & 0 deletions
58
...c/main/java/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.sentry.spring.jakarta.exception; | ||
|
||
import com.jakewharton.nopen.annotation.Open; | ||
import io.sentry.IHub; | ||
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.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 = "SentrySpring6CaptureExceptionParameterAdvice"; | ||
private final @NotNull IHub hub; | ||
|
||
public SentryCaptureExceptionParameterAdvice(final @NotNull IHub hub) { | ||
this.hub = Objects.requireNonNull(hub, "hub is required"); | ||
} | ||
|
||
@Override | ||
public Object invoke(final @NotNull MethodInvocation invocation) throws Throwable { | ||
final Method mostSpecificMethod = | ||
AopUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass()); | ||
SentryCaptureExceptionParameter sentryCaptureExceptionParameter = | ||
AnnotationUtils.findAnnotation(mostSpecificMethod, SentryCaptureExceptionParameter.class); | ||
|
||
if (sentryCaptureExceptionParameter != null) { | ||
Object[] args = invocation.getArguments(); | ||
for (Object arg : args) { | ||
if (arg instanceof Exception) { | ||
captureException((Exception) arg); | ||
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()); | ||
hub.captureException(mechanismException); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...java/io/sentry/spring/jakarta/exception/SentryCaptureExceptionParameterConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.sentry.spring.jakarta.exception; | ||
|
||
import com.jakewharton.nopen.annotation.Open; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
|
||
/** | ||
* Provides infrastructure beans for capturing exceptions passed to bean methods annotated with | ||
* {@link SentryCaptureExceptionParameter}. | ||
*/ | ||
@Configuration | ||
@Import({ | ||
SentryExceptionParameterAdviceConfiguration.class, | ||
SentryCaptureExceptionParameterPointcutConfiguration.class | ||
}) | ||
@Open | ||
public class SentryCaptureExceptionParameterConfiguration {} |
28 changes: 28 additions & 0 deletions
28
...sentry/spring/jakarta/exception/SentryCaptureExceptionParameterPointcutConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.sentry.spring.jakarta.exception; | ||
|
||
import com.jakewharton.nopen.annotation.Open; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.aop.Pointcut; | ||
import org.springframework.aop.support.ComposablePointcut; | ||
import org.springframework.aop.support.annotation.AnnotationClassFilter; | ||
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** AOP pointcut configuration for {@link SentryCaptureExceptionParameter}. */ | ||
@Configuration(proxyBeanMethods = false) | ||
@Open | ||
public class SentryCaptureExceptionParameterPointcutConfiguration { | ||
|
||
/** | ||
* Pointcut around which spans are created. | ||
* | ||
* @return pointcut used by {@link SentryCaptureExceptionParameterAdvice}. | ||
*/ | ||
@Bean | ||
public @NotNull Pointcut sentryCaptureExceptionParameterPointcut() { | ||
return new ComposablePointcut( | ||
new AnnotationClassFilter(SentryCaptureExceptionParameter.class, true)) | ||
.union(new AnnotationMatchingPointcut(null, SentryCaptureExceptionParameter.class)); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
.../java/io/sentry/spring/jakarta/exception/SentryExceptionParameterAdviceConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.sentry.spring.jakarta.exception; | ||
|
||
import com.jakewharton.nopen.annotation.Open; | ||
import io.sentry.IHub; | ||
import org.aopalliance.aop.Advice; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.aop.Advisor; | ||
import org.springframework.aop.Pointcut; | ||
import org.springframework.aop.support.DefaultPointcutAdvisor; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** Creates advice infrastructure for {@link SentryCaptureExceptionParameter}. */ | ||
@Configuration(proxyBeanMethods = false) | ||
@Open | ||
public class SentryExceptionParameterAdviceConfiguration { | ||
|
||
@Bean | ||
public @NotNull Advice sentryCaptureExceptionParameterAdvice(final @NotNull IHub hub) { | ||
return new SentryCaptureExceptionParameterAdvice(hub); | ||
} | ||
|
||
@Bean | ||
public @NotNull Advisor sentryCaptureExceptionParameterAdvisor( | ||
final @NotNull @Qualifier("sentryCaptureExceptionParameterPointcut") Pointcut | ||
sentryCaptureExceptionParameterPointcut, | ||
final @NotNull @Qualifier("sentryCaptureExceptionParameterAdvice") Advice | ||
sentryCaptureExceptionParameterAdvice) { | ||
return new DefaultPointcutAdvisor( | ||
sentryCaptureExceptionParameterPointcut, sentryCaptureExceptionParameterAdvice); | ||
} | ||
} |
Oops, something went wrong.