-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds a FilterChainExceptionHandlerFilter that allows to handle an exception thrown from a Filter in the same way as we do for exceptions thrown from controllers. It is not enabled by default for backwards compatibility. Use `error.handling.handle-filter-chain-exceptions=true` to enable it. Fixes #87
- Loading branch information
1 parent
0d373d1
commit f717389
Showing
9 changed files
with
225 additions
and
85 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
45 changes: 45 additions & 0 deletions
45
src/main/java/io/github/wimdeblauwe/errorhandlingspringbootstarter/ErrorHandlingFacade.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,45 @@ | ||
package io.github.wimdeblauwe.errorhandlingspringbootstarter; | ||
|
||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ErrorHandlingFacade { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ErrorHandlingFacade.class); | ||
|
||
private final List<ApiExceptionHandler> handlers; | ||
private final FallbackApiExceptionHandler fallbackHandler; | ||
private final LoggingService loggingService; | ||
private final List<ApiErrorResponseCustomizer> responseCustomizers; | ||
|
||
public ErrorHandlingFacade(List<ApiExceptionHandler> handlers, FallbackApiExceptionHandler fallbackHandler, LoggingService loggingService, | ||
List<ApiErrorResponseCustomizer> responseCustomizers) { | ||
this.handlers = handlers; | ||
this.fallbackHandler = fallbackHandler; | ||
this.loggingService = loggingService; | ||
this.responseCustomizers = responseCustomizers; | ||
} | ||
|
||
public ApiErrorResponse handle(Throwable exception) { | ||
ApiErrorResponse errorResponse = null; | ||
for (ApiExceptionHandler handler : handlers) { | ||
if (handler.canHandle(exception)) { | ||
errorResponse = handler.handle(exception); | ||
break; | ||
} | ||
} | ||
|
||
if (errorResponse == null) { | ||
errorResponse = fallbackHandler.handle(exception); | ||
} | ||
|
||
for (ApiErrorResponseCustomizer responseCustomizer : responseCustomizers) { | ||
responseCustomizer.customize(errorResponse); | ||
} | ||
|
||
loggingService.logException(errorResponse, exception); | ||
|
||
return errorResponse; | ||
} | ||
} |
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
42 changes: 6 additions & 36 deletions
42
...hub/wimdeblauwe/errorhandlingspringbootstarter/servlet/ErrorHandlingControllerAdvice.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
37 changes: 37 additions & 0 deletions
37
...wimdeblauwe/errorhandlingspringbootstarter/servlet/FilterChainExceptionHandlerFilter.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,37 @@ | ||
package io.github.wimdeblauwe.errorhandlingspringbootstarter.servlet; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.github.wimdeblauwe.errorhandlingspringbootstarter.ApiErrorResponse; | ||
import io.github.wimdeblauwe.errorhandlingspringbootstarter.ErrorHandlingFacade; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
public class FilterChainExceptionHandlerFilter extends OncePerRequestFilter { | ||
|
||
private final ErrorHandlingFacade errorHandlingFacade; | ||
private final ObjectMapper objectMapper; | ||
|
||
public FilterChainExceptionHandlerFilter(ErrorHandlingFacade errorHandlingFacade, ObjectMapper objectMapper) { | ||
this.errorHandlingFacade = errorHandlingFacade; | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
try { | ||
filterChain.doFilter(request, response); | ||
} catch (Exception ex) { | ||
ApiErrorResponse errorResponse = errorHandlingFacade.handle(ex); | ||
response.setStatus(errorResponse.getHttpStatus().value()); | ||
var jsonResponseBody = objectMapper.writeValueAsString(errorResponse); | ||
response.getWriter().write(jsonResponseBody); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
wimdeblauwe
Author
Owner
|
||
} | ||
} | ||
} |
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
Oops, something went wrong.
Unless I'm missing something, I think you should only call
errorHandlingFacade.handle(ex)
if theisHandleFilterChainExceptions()
config property is true. As far as I can tell, you're not using this new property anywhere. I was expecting this code to look like