-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggested change to fix logging of handled exceptions #166
Closed
TobiasMende
wants to merge
2
commits into
bugsnag:master
from
TobiasMende:156-place-bugnsag-resolver-just-before-default-handler
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.WebMvcConfigurerAdapter; | ||
|
||
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 WebMvcConfigurerAdapter { | ||
|
||
@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 | ||
public 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 { | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell the default exception resolvers are populated here.
My concern with this approach is that we don't have any control over the contents of this list. If the order or content of the default exception handlers is changed in the spring libraries then it could affect the way that the Bugsnag library works. If it does then we might end up in the situation of trying to support multiple versions of this mechanism as we can't guarantee what versions of the spring framework users are using.
Although there are problems with the current implementation, I believe that it is more resilient to change in the framework, and it's still possible to exclude unwanted exceptions using the
beforeNotify
callback, or by specifying "Discard by error class" in the project settings.