Class SuperControllerExceptionHandler

java.lang.Object
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
dev.daymor.sakuraboot.exceptions.SuperControllerExceptionHandler
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.MessageSourceAware

@EnableConfigurationProperties(ExceptionConfiguration.class) @RestControllerAdvice public class SuperControllerExceptionHandler extends org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
Global exception handler for the REST controller. Extends ResponseEntityExceptionHandler to provide custom handling of various exceptions and return appropriate error responses.

NOTE: This class already has all the needed methods to handle various exceptions. If you need to add other methods or modify existing one you can create a class that extends this one. All the methods are protected for this purpose.

Since:
0.1.0
  • Field Summary

    Fields inherited from class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler

    logger, PAGE_NOT_FOUND_LOG_CATEGORY, pageNotFoundLogger
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected Map<String,Object>
    getAdditionalValues(Exception ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
    Gets additional values to be included in the error response based on the provided exception and request.
    protected org.springframework.http.ResponseEntity<Object>
    handleConstraintViolation(RuntimeException ex, org.springframework.web.context.request.WebRequest request)
    Handles ConstraintViolationException, DataIntegrityViolationException and returns an appropriate error response.
    protected org.springframework.http.ResponseEntity<Object>
    handleException(Exception ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
    Handles generic exceptions with the provided status code and message.
    protected org.springframework.http.ResponseEntity<Object>
    handleException(Exception ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request, String message)
    Handles generic exceptions with the provided status code and message.
    protected org.springframework.http.ResponseEntity<Object>
    handleException(Exception ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request, String message, Map<String,Object> additionalValues)
    Handles generic exceptions with the provided status code and custom message.
    protected org.springframework.http.ResponseEntity<Object>
    handleHttpMediaTypeNotSupported(org.springframework.web.HttpMediaTypeNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
    Handles HttpMediaTypeNotSupportedException and returns an appropriate error response.
    protected org.springframework.http.ResponseEntity<Object>
    handleHttpMessageNotReadable(org.springframework.http.converter.HttpMessageNotReadableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
    Handles HttpMessageNotReadableException and returns an appropriate error response.
    protected org.springframework.http.ResponseEntity<Object>
    handleHttpRequestMethodNotSupported(org.springframework.web.HttpRequestMethodNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
    Handles HttpRequestMethodNotSupportedException and returns an appropriate error response.
    protected org.springframework.http.ResponseEntity<Object>
    handleMethodArgumentNotValid(org.springframework.web.bind.MethodArgumentNotValidException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
    Handles MethodArgumentNotValidException and returns an appropriate error response.
    protected org.springframework.http.ResponseEntity<Object>
    handleMissingPathVariable(org.springframework.web.bind.MissingPathVariableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
    Handles MissingPathVariableException and returns an appropriate error response.
    protected org.springframework.http.ResponseEntity<Object>
    handleMultipartException(org.springframework.web.multipart.MultipartException ex, org.springframework.web.context.request.WebRequest request)
    Handles MultipartException and returns an appropriate error response.
    protected org.springframework.http.ResponseEntity<Object>
    handleNoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
    Handles NoHandlerFoundException and returns an appropriate error response.
    protected org.springframework.http.ResponseEntity<Object>
    handleOtherException(Exception ex, org.springframework.web.context.request.WebRequest request)
    Handles generic Exception and returns an appropriate error response.
    protected org.springframework.http.ResponseEntity<Object>
    handleSuperException(SuperException ex, org.springframework.web.context.request.WebRequest request)
    Handles SuperException and returns an appropriate error response.
    protected org.springframework.http.ResponseEntity<Object>
    handleTypeMismatch(org.springframework.beans.TypeMismatchException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
    Handles TypeMismatchException and returns an appropriate error response.

    Methods inherited from class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler

    createProblemDetail, createResponseEntity, getMessageSource, handleAsyncRequestTimeoutException, handleBindException, handleConversionNotSupported, handleErrorResponseException, handleException, handleExceptionInternal, handleHandlerMethodValidationException, handleHttpMediaTypeNotAcceptable, handleHttpMessageNotWritable, handleMaxUploadSizeExceededException, handleMethodValidationException, handleMissingServletRequestParameter, handleMissingServletRequestPart, handleNoResourceFoundException, handleServletRequestBindingException, setMessageSource

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SuperControllerExceptionHandler

      public SuperControllerExceptionHandler()
  • Method Details

    • handleException

      @Nullable protected org.springframework.http.ResponseEntity<Object> handleException(Exception ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
      Handles generic exceptions with the provided status code and message.
      Parameters:
      ex - The exception to handle.
      headers - The HTTP headers.
      status - The HTTP status code.
      request - The web request.
      Returns:
      ResponseEntity containing the error response.
    • handleException

      @Nullable protected org.springframework.http.ResponseEntity<Object> handleException(Exception ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request, String message)
      Handles generic exceptions with the provided status code and message.
      Parameters:
      ex - The exception to handle.
      headers - The HTTP headers.
      status - The HTTP status code.
      request - The web request.
      message - The custom message.
      Returns:
      ResponseEntity containing the error response.
    • handleException

      @Nullable protected org.springframework.http.ResponseEntity<Object> handleException(Exception ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request, String message, @Nullable Map<String,Object> additionalValues)
      Handles generic exceptions with the provided status code and custom message.
      Parameters:
      ex - The exception to handle.
      headers - The HTTP headers.
      status - The HTTP status code.
      request - The web request.
      message - The custom message.
      additionalValues - Additional values to be included in the error.
      Returns:
      ResponseEntity containing the error response.
    • getAdditionalValues

      protected Map<String,Object> getAdditionalValues(Exception ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
      Gets additional values to be included in the error response based on the provided exception and request.
      Parameters:
      ex - The exception.
      headers - The HTTP headers.
      status - The HTTP status code.
      request - The web request.
      Returns:
      Additional values to be included in the error response.
    • handleHttpRequestMethodNotSupported

      protected org.springframework.http.ResponseEntity<Object> handleHttpRequestMethodNotSupported(org.springframework.web.HttpRequestMethodNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
      Handles HttpRequestMethodNotSupportedException and returns an appropriate error response.
      Overrides:
      handleHttpRequestMethodNotSupported in class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
      Parameters:
      ex - The exception to handle.
      headers - The HTTP headers.
      status - The HTTP status code.
      request - The web request.
      Returns:
      ResponseEntity containing the error response.
    • handleNoHandlerFoundException

      protected org.springframework.http.ResponseEntity<Object> handleNoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
      Handles NoHandlerFoundException and returns an appropriate error response.
      Overrides:
      handleNoHandlerFoundException in class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
      Parameters:
      ex - The exception to handle.
      headers - The HTTP headers.
      status - The HTTP status code.
      request - The web request.
      Returns:
      ResponseEntity containing the error response.
    • handleMethodArgumentNotValid

      protected org.springframework.http.ResponseEntity<Object> handleMethodArgumentNotValid(org.springframework.web.bind.MethodArgumentNotValidException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
      Handles MethodArgumentNotValidException and returns an appropriate error response.
      Overrides:
      handleMethodArgumentNotValid in class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
      Parameters:
      ex - The exception to handle.
      headers - The HTTP headers.
      status - The HTTP status code.
      request - The web request.
      Returns:
      ResponseEntity containing the error response.
    • handleConstraintViolation

      @Nullable @ExceptionHandler({jakarta.validation.ConstraintViolationException.class,org.springframework.dao.DataIntegrityViolationException.class}) protected org.springframework.http.ResponseEntity<Object> handleConstraintViolation(RuntimeException ex, org.springframework.web.context.request.WebRequest request)
      Handles ConstraintViolationException, DataIntegrityViolationException and returns an appropriate error response.
      Parameters:
      ex - The exception to handle.
      request - The web request.
      Returns:
      ResponseEntity containing the error response.
    • handleHttpMediaTypeNotSupported

      protected org.springframework.http.ResponseEntity<Object> handleHttpMediaTypeNotSupported(org.springframework.web.HttpMediaTypeNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
      Handles HttpMediaTypeNotSupportedException and returns an appropriate error response.
      Overrides:
      handleHttpMediaTypeNotSupported in class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
      Parameters:
      ex - The exception to handle.
      headers - The HTTP headers.
      status - The HTTP status code.
      request - The web request.
      Returns:
      ResponseEntity containing the error response.
    • handleTypeMismatch

      protected org.springframework.http.ResponseEntity<Object> handleTypeMismatch(org.springframework.beans.TypeMismatchException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
      Handles TypeMismatchException and returns an appropriate error response.
      Overrides:
      handleTypeMismatch in class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
      Parameters:
      ex - The exception to handle.
      headers - The HTTP headers.
      status - The HTTP status code.
      request - The web request.
      Returns:
      ResponseEntity containing the error response.
    • handleMissingPathVariable

      protected org.springframework.http.ResponseEntity<Object> handleMissingPathVariable(org.springframework.web.bind.MissingPathVariableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
      Handles MissingPathVariableException and returns an appropriate error response.
      Overrides:
      handleMissingPathVariable in class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
      Parameters:
      ex - The exception to handle.
      headers - The HTTP headers.
      status - The HTTP status code.
      request - The web request.
      Returns:
      ResponseEntity containing the error response.
    • handleMultipartException

      @Nullable @ExceptionHandler(org.springframework.web.multipart.MultipartException.class) protected org.springframework.http.ResponseEntity<Object> handleMultipartException(org.springframework.web.multipart.MultipartException ex, org.springframework.web.context.request.WebRequest request)
      Handles MultipartException and returns an appropriate error response.
      Parameters:
      ex - The exception to handle.
      request - The web request.
      Returns:
      ResponseEntity containing the error response.
    • handleHttpMessageNotReadable

      protected org.springframework.http.ResponseEntity<Object> handleHttpMessageNotReadable(org.springframework.http.converter.HttpMessageNotReadableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
      Handles HttpMessageNotReadableException and returns an appropriate error response.
      Overrides:
      handleHttpMessageNotReadable in class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
      Parameters:
      ex - The exception to handle.
      headers - The HTTP headers.
      status - The HTTP status code.
      request - The web request.
      Returns:
      ResponseEntity containing the error response.
    • handleSuperException

      @Nullable @ExceptionHandler(SuperException.class) protected org.springframework.http.ResponseEntity<Object> handleSuperException(SuperException ex, org.springframework.web.context.request.WebRequest request)
      Handles SuperException and returns an appropriate error response.
      Parameters:
      ex - The exception to handle.
      request - The web request.
      Returns:
      ResponseEntity containing the error response.
    • handleOtherException

      @Nullable @ExceptionHandler(java.lang.Exception.class) protected org.springframework.http.ResponseEntity<Object> handleOtherException(Exception ex, org.springframework.web.context.request.WebRequest request)
      Handles generic Exception and returns an appropriate error response.
      Parameters:
      ex - The exception to handle.
      request - The web request.
      Returns:
      ResponseEntity containing the error response.