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.Aware, org.springframework.beans.factory.InitializingBean, org.springframework.context.MessageSourceAware

@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
  • Constructor Details

  • Method Details

    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.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)
      Handles HttpException instances.

      Each HttpException has an associated HttpStatus that is used as the response status.

      Parameters:
      e - the exception
      request - the current request
      Returns:
      a response entity reflecting the current exception
    • handleMethodArgumentNotValid

      protected org.springframework.http.ResponseEntity<Object> handleMethodArgumentNotValid(@NonNull org.springframework.web.bind.MethodArgumentNotValidException ex, @NonNull org.springframework.http.HttpHeaders headers, @NonNull org.springframework.http.HttpStatusCode status, @NonNull org.springframework.web.context.request.WebRequest request)
      Handles MethodArgumentNotValidException instances.

      The response status is: 400 Bad Request.

      Overrides:
      handleMethodArgumentNotValid in class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
      Parameters:
      ex - the exception
      request - the current request
      Returns:
      a response entity reflecting the current exception
    • accessDeniedException

      @ExceptionHandler(org.springframework.security.access.AccessDeniedException.class) @ResponseBody public org.springframework.http.ResponseEntity<Object> accessDeniedException(org.springframework.security.access.AccessDeniedException ex, org.springframework.web.context.request.WebRequest request)
      Handles AccessDeniedException instances.

      The response status is: 403 Forbidden.

      Parameters:
      ex - the exception
      request - 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)
      Handles ValidationException instances.

      The response status is: 400 Bad Request.

      Parameters:
      e - the exception
      request - 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)
      Handles ValidationException instances.

      The response status is: 400 Bad Request.

      Parameters:
      e - the exception
      request - 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)
      Handles Throwable instances. This method acts as a fallback handler.
      Parameters:
      t - the exception
      request - the current request
      Returns:
      a response entity reflecting the current exception
    • 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 using exceptionResponseFactory and then applies the error response enrichers returned from getResponseEnrichers() to complete the response.

      Parameters:
      throwable - the exception
      status - the HTTP status
      request - the current request
      Returns:
      an error response
    • addResponseEnricher

      protected void addResponseEnricher(ErrorResponseEnricher errorResponseEnricher)
      Parameters:
      errorResponseEnricher - the error response enricher
    • removeResponseEnricher

      protected void removeResponseEnricher(ErrorResponseEnricher errorResponseEnricher)
      De-registers a ErrorResponseEnricher.
      Parameters:
      errorResponseEnricher - the error response enricher
    • removeResponseEnricher

      protected void removeResponseEnricher(Class<? extends ErrorResponseEnricher> className)
      De-registers a ErrorResponseEnricher based on its class name.
      Parameters:
      className - the class name of the ErrorResponseEnricher 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) and removeResponseEnricher(ErrorResponseEnricher).

      The default implementation adds the following listeners:

    • getResponseEnrichers

      protected Collection<ErrorResponseEnricher> getResponseEnrichers()
      Returns a collection of registered response enrichers.
      Returns:
      the response enrichers