Class 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
    • Method Detail

      • 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
      • 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
      • 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 class org.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 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
      • removeResponseEnricher

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

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