-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Insert BugsnagMvcExceptionHandler just before the DefaultHandlerExcep…
…tionResolver
- Loading branch information
Tobias Mende
committed
Jul 21, 2021
1 parent
81abe4c
commit a5e903a
Showing
7 changed files
with
91 additions
and
16 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
34 changes: 23 additions & 11 deletions
34
bugsnag-spring/src/main/java/com/bugsnag/MvcConfiguration.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 |
---|---|---|
@@ -1,36 +1,48 @@ | ||
package com.bugsnag; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.HandlerExceptionResolver; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; | ||
|
||
import java.util.List; | ||
import javax.annotation.PostConstruct; | ||
|
||
/** | ||
* If spring-webmvc is loaded, add configuration for reporting unhandled exceptions. | ||
*/ | ||
@Configuration | ||
@Conditional(SpringWebMvcLoadedCondition.class) | ||
class MvcConfiguration { | ||
class MvcConfiguration extends WebMvcConfigurationSupport { | ||
|
||
@Autowired | ||
private Bugsnag bugsnag; | ||
|
||
/** | ||
* Register an exception resolver to send unhandled reports to Bugsnag | ||
* for uncaught exceptions thrown from request handlers. | ||
*/ | ||
@Bean | ||
BugsnagMvcExceptionHandler bugsnagHandlerExceptionResolver() { | ||
return new BugsnagMvcExceptionHandler(bugsnag); | ||
} | ||
|
||
/** | ||
* Add a callback to assign specified severities for some Spring exceptions. | ||
*/ | ||
@PostConstruct | ||
void addExceptionClassCallback() { | ||
bugsnag.addCallback(new ExceptionClassCallback()); | ||
} | ||
|
||
/** | ||
* Normally, the exceptionResolvers contain the following resolvers in this order: | ||
* - {@link org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver} | ||
* - {@link org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver} | ||
* - {@link org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver} | ||
* <p> | ||
* The first two handlers handle exceptions in an application-specific manner. | ||
* (either by @{@link org.springframework.web.bind.annotation.ExceptionHandler} | ||
* or @{@link org.springframework.web.bind.annotation.ResponseStatus}) | ||
* <p> | ||
* Therefore, exceptions that are handled by these handlers should not be handled by Bugsnag. | ||
* Only unhandled exceptions shall be sent to Bugsnag. | ||
*/ | ||
@Override | ||
protected void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { | ||
final int position = exceptionResolvers.isEmpty() ? 0 : exceptionResolvers.size() - 1; | ||
exceptionResolvers.add(position, new BugsnagMvcExceptionHandler(bugsnag)); | ||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
bugsnag-spring/src/test/java/com/bugsnag/testapp/springboot/TestCustomException.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,4 @@ | ||
package com.bugsnag.testapp.springboot; | ||
|
||
public class TestCustomException extends RuntimeException { | ||
} |
13 changes: 13 additions & 0 deletions
13
bugsnag-spring/src/test/java/com/bugsnag/testapp/springboot/TestExceptionHandler.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,13 @@ | ||
package com.bugsnag.testapp.springboot; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
@ControllerAdvice | ||
public class TestExceptionHandler { | ||
@ExceptionHandler(TestCustomException.class) | ||
public ResponseEntity handleTestCustomException(TestCustomException ignored) { | ||
return ResponseEntity.ok(TestCustomException.class.getSimpleName()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
bugsnag-spring/src/test/java/com/bugsnag/testapp/springboot/TestResponseStatusException.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,9 @@ | ||
package com.bugsnag.testapp.springboot; | ||
|
||
import static org.springframework.http.HttpStatus.I_AM_A_TEAPOT; | ||
|
||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(I_AM_A_TEAPOT) | ||
public class TestResponseStatusException extends RuntimeException { | ||
} |