Class HawaiiResponseEntityExceptionHandler
- java.lang.Object
-
- org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
-
- org.hawaiiframework.web.exception.HawaiiResponseEntityExceptionHandler
-
- All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean
@ControllerAdvice public class HawaiiResponseEntityExceptionHandler extends org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler implements org.springframework.beans.factory.InitializingBean
This class creates proper HTTP response bodies for exceptions.In this implementation, the response enrichers are stored in a map, with the class name as key. This means that any enricher can be stored only once. Also, enrichers are not ordered. Subclasses may implement another mechanism if required. This would mean that the following methods would need to be overwritten:
- Since:
- 2.0.0
- Author:
- Marcel Overdijk, Ivan Melotte, Paul Klos, Richard den Adel
-
-
Constructor Summary
Constructors Constructor Description HawaiiResponseEntityExceptionHandler(ModelConverter<ValidationError,ValidationErrorResource> validationErrorResourceAssembler, ExceptionResponseFactory exceptionResponseFactory)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addResponseEnricher(ErrorResponseEnricher errorResponseEnricher)
Registers aErrorResponseEnricher
.void
afterPropertiesSet()
protected ErrorResponseResource
buildErrorResponseBody(Throwable throwable, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request)
Builds a meaningful response body for the given throwable, HTTP status and request.protected void
configureResponseEnrichers()
Configures the error response enrichers.protected Collection<ErrorResponseEnricher>
getResponseEnrichers()
Returns a collection of registered response enrichers.org.springframework.http.ResponseEntity<Object>
handleApiException(ApiException e, org.springframework.web.context.request.WebRequest request)
HandlesValidationException
instances.protected org.springframework.http.ResponseEntity<Object>
handleExceptionInternal(Exception ex, Object body, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request)
org.springframework.http.ResponseEntity<Object>
handleHttpException(HttpException e, org.springframework.web.context.request.WebRequest request)
HandlesHttpException
instances.org.springframework.http.ResponseEntity<Object>
handleThrowable(Throwable t, org.springframework.web.context.request.WebRequest request)
HandlesThrowable
instances.org.springframework.http.ResponseEntity<Object>
handleValidationException(ValidationException e, org.springframework.web.context.request.WebRequest request)
HandlesValidationException
instances.protected void
removeResponseEnricher(Class<? extends ErrorResponseEnricher> className)
De-registers aErrorResponseEnricher
based on its class name.protected void
removeResponseEnricher(ErrorResponseEnricher errorResponseEnricher)
De-registers aErrorResponseEnricher
.-
Methods inherited from class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
handleAsyncRequestTimeoutException, handleBindException, handleConversionNotSupported, handleException, handleHttpMediaTypeNotAcceptable, handleHttpMediaTypeNotSupported, handleHttpMessageNotReadable, handleHttpMessageNotWritable, handleHttpRequestMethodNotSupported, handleMethodArgumentNotValid, handleMissingPathVariable, handleMissingServletRequestParameter, handleMissingServletRequestPart, handleNoHandlerFoundException, handleServletRequestBindingException, handleTypeMismatch
-
-
-
-
Constructor Detail
-
HawaiiResponseEntityExceptionHandler
public HawaiiResponseEntityExceptionHandler(ModelConverter<ValidationError,ValidationErrorResource> validationErrorResourceAssembler, ExceptionResponseFactory exceptionResponseFactory)
-
-
Method Detail
-
afterPropertiesSet
public void afterPropertiesSet()
- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
-
handleHttpException
@ExceptionHandler(HttpException.class) @ResponseBody public org.springframework.http.ResponseEntity<Object> handleHttpException(HttpException e, org.springframework.web.context.request.WebRequest request)
HandlesHttpException
instances.Each
HttpException
has an associatedHttpStatus
that is used as the response status.- Parameters:
e
- the exceptionrequest
- the current request- Returns:
- a response entity reflecting the current exception
-
handleValidationException
@ExceptionHandler(ValidationException.class) @ResponseBody public org.springframework.http.ResponseEntity<Object> handleValidationException(ValidationException e, org.springframework.web.context.request.WebRequest request)
HandlesValidationException
instances.The response status is: 400 Bad Request.
- Parameters:
e
- the exceptionrequest
- the current request- Returns:
- a response entity reflecting the current exception
-
handleApiException
@ExceptionHandler(ApiException.class) @ResponseBody public org.springframework.http.ResponseEntity<Object> handleApiException(ApiException e, org.springframework.web.context.request.WebRequest request)
HandlesValidationException
instances.The response status is: 400 Bad Request.
- Parameters:
e
- the exceptionrequest
- the current request- Returns:
- a response entity reflecting the current exception
-
handleThrowable
@ExceptionHandler(java.lang.Throwable.class) @ResponseBody public org.springframework.http.ResponseEntity<Object> handleThrowable(Throwable t, org.springframework.web.context.request.WebRequest request)
HandlesThrowable
instances. This method acts as a fallback handler.- Parameters:
t
- the exceptionrequest
- the current request- Returns:
- a response entity reflecting the current exception
-
handleExceptionInternal
@NonNull protected org.springframework.http.ResponseEntity<Object> handleExceptionInternal(@NonNull Exception ex, Object body, @Nullable org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, @NonNull org.springframework.web.context.request.WebRequest request)
- Overrides:
handleExceptionInternal
in classorg.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
-
buildErrorResponseBody
protected ErrorResponseResource buildErrorResponseBody(Throwable throwable, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request)
Builds a meaningful response body for the given throwable, HTTP status and request.This method constructs an
ErrorResponseResource
usingexceptionResponseFactory
and then applies the error response enrichers returned fromgetResponseEnrichers()
to complete the response.- Parameters:
throwable
- the exceptionstatus
- the HTTP statusrequest
- the current request- Returns:
- an error response
-
addResponseEnricher
protected void addResponseEnricher(ErrorResponseEnricher errorResponseEnricher)
Registers aErrorResponseEnricher
.- Parameters:
errorResponseEnricher
- the error response enricher
-
removeResponseEnricher
protected void removeResponseEnricher(ErrorResponseEnricher errorResponseEnricher)
De-registers aErrorResponseEnricher
.- Parameters:
errorResponseEnricher
- the error response enricher
-
removeResponseEnricher
protected void removeResponseEnricher(Class<? extends ErrorResponseEnricher> className)
De-registers aErrorResponseEnricher
based on its class name.- Parameters:
className
- the class name of theErrorResponseEnricher
to remove
-
configureResponseEnrichers
protected void configureResponseEnrichers()
Configures the error response enrichers.Subclasses may override this method to remove existing or add additional listeners, using
addResponseEnricher(ErrorResponseEnricher)
andremoveResponseEnricher(ErrorResponseEnricher)
.The default implementation adds the following listeners:
-
getResponseEnrichers
protected Collection<ErrorResponseEnricher> getResponseEnrichers()
Returns a collection of registered response enrichers.- Returns:
- the response enrichers
-
-