Package com.myhrcrmproject.controller
Class GlobalExceptionHandler
java.lang.Object
com.myhrcrmproject.controller.GlobalExceptionHandler
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ResponseEntity<String>HandlesAlreadyExistsExceptionand returns a 400 Bad Request.org.springframework.http.ResponseEntity<String>handlerConstraintViolationException(jakarta.validation.ConstraintViolationException e) HandlesConstraintViolationExceptionand returns a 400 Bad Request.org.springframework.http.ResponseEntity<String>handlerDataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException e) HandlesDataIntegrityViolationExceptionand returns a 400 Bad Request with a custom message.org.springframework.http.ResponseEntity<String>HandlesInvalidJwtExceptionand returns a 451 Unavailable For Legal Reasons.org.springframework.http.ResponseEntity<String>handlerNotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException e) HandlesNotAcceptableStatusExceptionand returns a 403 Forbidden.org.springframework.http.ResponseEntity<String>HandlesNotFoundExceptionand returns a 404 Not Found.org.springframework.http.ResponseEntity<String>HandlesNullPointerExceptionand returns a 500 Internal Server Error.org.springframework.http.ResponseEntity<String>HandlesSQLIntegrityConstraintViolationExceptionand returns a 500 Internal Server Error.
-
Constructor Details
-
GlobalExceptionHandler
public GlobalExceptionHandler()
-
-
Method Details
-
handlerNullPointerException
@ExceptionHandler(java.lang.NullPointerException.class) public org.springframework.http.ResponseEntity<String> handlerNullPointerException(NullPointerException e) HandlesNullPointerExceptionand returns a 500 Internal Server Error.- Parameters:
e- The exception to handle.- Returns:
- A
ResponseEntitywith an error message and HTTP status code 500.
-
handlerSQLIntegrityConstraintViolationException
@ExceptionHandler(java.sql.SQLIntegrityConstraintViolationException.class) public org.springframework.http.ResponseEntity<String> handlerSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) HandlesSQLIntegrityConstraintViolationExceptionand returns a 500 Internal Server Error.- Parameters:
e- The exception to handle.- Returns:
- A
ResponseEntitywith 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) HandlesConstraintViolationExceptionand returns a 400 Bad Request.- Parameters:
e- The exception to handle.- Returns:
- A
ResponseEntitywith an error message and HTTP status code 400.
-
handlerAlreadyExistsException
@ExceptionHandler(AlreadyExistsException.class) public org.springframework.http.ResponseEntity<String> handlerAlreadyExistsException(AlreadyExistsException e) HandlesAlreadyExistsExceptionand returns a 400 Bad Request.- Parameters:
e- The exception to handle.- Returns:
- A
ResponseEntitywith 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) HandlesDataIntegrityViolationExceptionand returns a 400 Bad Request with a custom message.- Parameters:
e- The exception to handle.- Returns:
- A
ResponseEntitywith a custom error message and HTTP status code 400.
-
handlerNotFoundException
@ExceptionHandler(NotFoundException.class) public org.springframework.http.ResponseEntity<String> handlerNotFoundException(NotFoundException e) HandlesNotFoundExceptionand returns a 404 Not Found.- Parameters:
e- The exception to handle.- Returns:
- A
ResponseEntitywith an error message and HTTP status code 404.
-
handlerInvalidJwtException
@ExceptionHandler(InvalidJwtException.class) public org.springframework.http.ResponseEntity<String> handlerInvalidJwtException(InvalidJwtException e) HandlesInvalidJwtExceptionand returns a 451 Unavailable For Legal Reasons.- Parameters:
e- The exception to handle.- Returns:
- A
ResponseEntitywith 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) HandlesNotAcceptableStatusExceptionand returns a 403 Forbidden.- Parameters:
e- The exception to handle.- Returns:
- A
ResponseEntitywith an error message and HTTP status code 403.
-