Class GlobalExceptionHandler

java.lang.Object
com.myhrcrmproject.controller.GlobalExceptionHandler

@ControllerAdvice public class GlobalExceptionHandler extends Object
Global exception handler for handling various exceptions that may occur during the execution of RESTful APIs in the HR CRM system.

This class is annotated with @ControllerAdvice, indicating that it assists all controllers in the application. It includes methods annotated with @ExceptionHandler to handle specific exceptions and return appropriate responses with HTTP status codes.

The supported exceptions include:

  • NullPointerException: Returns a 500 Internal Server Error.
  • SQLIntegrityConstraintViolationException: Returns a 500 Internal Server Error.
  • ConstraintViolationException: Returns a 400 Bad Request.
  • AlreadyExistsException: Returns a 400 Bad Request.
  • DataIntegrityViolationException: Returns a 400 Bad Request with a custom message.
  • NotFoundException: Returns a 404 Not Found.
  • InvalidJwtException: Returns a 451 Unavailable For Legal Reasons.
  • NotAcceptableStatusException: Returns a 403 Forbidden.
Version:
1.0
Author:
Denys Chaykovskyy
  • Constructor Details

    • GlobalExceptionHandler

      public GlobalExceptionHandler()
  • Method Details

    • handlerNullPointerException

      @ExceptionHandler(java.lang.NullPointerException.class) public org.springframework.http.ResponseEntity<String> handlerNullPointerException(NullPointerException e)
      Handles NullPointerException and returns a 500 Internal Server Error.
      Parameters:
      e - The exception to handle.
      Returns:
      A ResponseEntity with an error message and HTTP status code 500.
    • handlerSQLIntegrityConstraintViolationException

      @ExceptionHandler(java.sql.SQLIntegrityConstraintViolationException.class) public org.springframework.http.ResponseEntity<String> handlerSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e)
      Handles SQLIntegrityConstraintViolationException and returns a 500 Internal Server Error.
      Parameters:
      e - The exception to handle.
      Returns:
      A ResponseEntity with an error message and HTTP status code 500.
    • handlerConstraintViolationException

      @ExceptionHandler(jakarta.validation.ConstraintViolationException.class) public org.springframework.http.ResponseEntity<String> handlerConstraintViolationException(jakarta.validation.ConstraintViolationException e)
      Handles ConstraintViolationException and returns a 400 Bad Request.
      Parameters:
      e - The exception to handle.
      Returns:
      A ResponseEntity with an error message and HTTP status code 400.
    • handlerAlreadyExistsException

      @ExceptionHandler(AlreadyExistsException.class) public org.springframework.http.ResponseEntity<String> handlerAlreadyExistsException(AlreadyExistsException e)
      Handles AlreadyExistsException and returns a 400 Bad Request.
      Parameters:
      e - The exception to handle.
      Returns:
      A ResponseEntity with an error message and HTTP status code 400.
    • handlerDataIntegrityViolationException

      @ExceptionHandler(org.springframework.dao.DataIntegrityViolationException.class) public org.springframework.http.ResponseEntity<String> handlerDataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException e)
      Handles DataIntegrityViolationException and returns a 400 Bad Request with a custom message.
      Parameters:
      e - The exception to handle.
      Returns:
      A ResponseEntity with a custom error message and HTTP status code 400.
    • handlerNotFoundException

      @ExceptionHandler(NotFoundException.class) public org.springframework.http.ResponseEntity<String> handlerNotFoundException(NotFoundException e)
      Handles NotFoundException and returns a 404 Not Found.
      Parameters:
      e - The exception to handle.
      Returns:
      A ResponseEntity with an error message and HTTP status code 404.
    • handlerInvalidJwtException

      @ExceptionHandler(InvalidJwtException.class) public org.springframework.http.ResponseEntity<String> handlerInvalidJwtException(InvalidJwtException e)
      Handles InvalidJwtException and returns a 451 Unavailable For Legal Reasons.
      Parameters:
      e - The exception to handle.
      Returns:
      A ResponseEntity with an error message and HTTP status code 451.
    • handlerNotAcceptableStatusException

      @ExceptionHandler(org.springframework.web.server.NotAcceptableStatusException.class) public org.springframework.http.ResponseEntity<String> handlerNotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException e)
      Handles NotAcceptableStatusException and returns a 403 Forbidden.
      Parameters:
      e - The exception to handle.
      Returns:
      A ResponseEntity with an error message and HTTP status code 403.