-
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.
Merge pull request #91 from wimdeblauwe/feature/gh-87_handle-filter-c…
…hain-exceptions Handle filter chain exceptions
- Loading branch information
Showing
10 changed files
with
236 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
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 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.