Skip to content

24 added support to the RestExceptionHandler to access the HttpServle… #25

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
:repo-uri: https://github.com/jirutka/spring-rest-exception-handler
:issue-uri: {repo-uri}/issues

== 1.3.0 (2017-12-13)
* Added support to access the `HttpServletResponse` in

== 1.2.0 (2015-05-16)

* Modify `ErrorMessageRestExceptionHandler` to log missing message on the DEBUG level instead of INFO.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected ModelAndView doResolveException(

ResponseEntity<?> entity;
try {
entity = handleException(exception, request);
entity = handleException(exception, request, response);
} catch (NoExceptionHandlerFoundException ex) {
LOG.warn("No exception handler found to handle exception: {}", exception.getClass().getName());
return null;
Expand All @@ -129,15 +129,15 @@ protected ModelAndView doResolveException(
return new ModelAndView();
}

protected ResponseEntity<?> handleException(Exception exception, HttpServletRequest request) {
protected ResponseEntity<?> handleException(Exception exception, HttpServletRequest request, HttpServletResponse response) {
// See http://stackoverflow.com/a/12979543/2217862
// This attribute is never set in MockMvc, so it's not covered in integration test.
request.removeAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);

RestExceptionHandler<Exception, ?> handler = resolveExceptionHandler(exception.getClass());

LOG.debug("Handling exception {} with response factory: {}", exception.getClass().getName(), handler);
return handler.handleException(exception, request);
return handler.handleException(exception, request, response);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.http.ResponseEntity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Contract for classes generating a {@link ResponseEntity} for an instance of the specified
Expand All @@ -37,4 +38,17 @@ public interface RestExceptionHandler<E extends Exception, T> {
* @return A response entity.
*/
ResponseEntity<T> handleException(E exception, HttpServletRequest request);


/**
* Handles exception and generates {@link ResponseEntity}.
*
* @param exception The exception to handle and get data from.
* @param request The current request.
* @param response The current response.
* @return A response entity.
*/
default ResponseEntity<T> handleException(E exception, HttpServletRequest request, HttpServletResponse response) {
return handleException(exception, request);
}
}