Class GlobalDatabaseExceptionHandler
java.lang.Object
com.app.controller.exceptionsHandler.GlobalDatabaseExceptionHandler
Global exception handler for database-related exceptions across all controllers.
Handles common persistence exceptions and returns appropriate HTTP status codes
and standardized error responses.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionHandles generic database access exceptions not covered by more specific handlers.entityNotFoundException
(jakarta.persistence.EntityNotFoundException e) Handles the case when a requested entity is not found in the database.Handles data integrity violations such as unique constraint breaches.handleJpaSystemException
(org.springframework.orm.jpa.JpaSystemException e) Handles exceptions related to JPA system errors.Handles exceptions related to optimistic and pessimistic locking failures, including deadlocks or inability to acquire locks.Handles the case when a resource that is attempted to be created already exists.handleTransactionException
(org.springframework.transaction.TransactionSystemException e) Handles transaction-related exceptions that occur when a transaction fails.
-
Constructor Details
-
GlobalDatabaseExceptionHandler
public GlobalDatabaseExceptionHandler()
-
-
Method Details
-
entityNotFoundException
@ExceptionHandler(jakarta.persistence.EntityNotFoundException.class) @ResponseStatus(NOT_FOUND) public ResponseDto<String> entityNotFoundException(jakarta.persistence.EntityNotFoundException e) Handles the case when a requested entity is not found in the database. Returns HTTP 404 NOT FOUND.- Parameters:
e
- the EntityNotFoundException thrown by JPA- Returns:
- a response containing the error message
-
handleDataIntegrityViolation
@ExceptionHandler({org.springframework.dao.DataIntegrityViolationException.class,org.hibernate.exception.ConstraintViolationException.class}) @ResponseStatus(CONFLICT) public ResponseDto<String> handleDataIntegrityViolation(Exception e) Handles data integrity violations such as unique constraint breaches. Returns HTTP 409 CONFLICT.- Parameters:
e
- the exception representing data integrity violation- Returns:
- a generic error message about data integrity violation
-
handleLockingExceptions
@ExceptionHandler({org.springframework.orm.ObjectOptimisticLockingFailureException.class,org.springframework.dao.PessimisticLockingFailureException.class,org.springframework.dao.CannotAcquireLockException.class,org.springframework.dao.DeadlockLoserDataAccessException.class}) @ResponseStatus(CONFLICT) public ResponseDto<String> handleLockingExceptions(Exception e) Handles exceptions related to optimistic and pessimistic locking failures, including deadlocks or inability to acquire locks. Returns HTTP 409 CONFLICT.- Parameters:
e
- the exception indicating locking conflict- Returns:
- a user-friendly message advising to retry the operation
-
handleTransactionException
@ExceptionHandler(org.springframework.transaction.TransactionSystemException.class) @ResponseStatus(INTERNAL_SERVER_ERROR) public ResponseDto<String> handleTransactionException(org.springframework.transaction.TransactionSystemException e) Handles transaction-related exceptions that occur when a transaction fails. Returns HTTP 500 INTERNAL SERVER ERROR.- Parameters:
e
- the TransactionSystemException thrown on transaction failure- Returns:
- a generic transaction failure message
-
handleJpaSystemException
@ExceptionHandler(org.springframework.orm.jpa.JpaSystemException.class) @ResponseStatus(INTERNAL_SERVER_ERROR) public ResponseDto<String> handleJpaSystemException(org.springframework.orm.jpa.JpaSystemException e) Handles exceptions related to JPA system errors. Returns HTTP 500 INTERNAL SERVER ERROR.- Parameters:
e
- the JpaSystemException representing JPA system errors- Returns:
- a generic database operation error message
-
handleResourceAlreadyExistException
@ExceptionHandler(ResourceAlreadyExistException.class) @ResponseStatus(CONFLICT) public ResponseDto<String> handleResourceAlreadyExistException(ResourceAlreadyExistException e) Handles the case when a resource that is attempted to be created already exists. Returns HTTP 409 CONFLICT.- Parameters:
e
- the ResourceAlreadyExistException indicating resource duplication- Returns:
- the error message from the exception
-
dataBaseException
@ExceptionHandler(org.springframework.dao.DataAccessException.class) @ResponseStatus(UNAUTHORIZED) public ResponseDto<String> dataBaseException(Exception e) Handles generic database access exceptions not covered by more specific handlers. Returns HTTP 401 UNAUTHORIZED.- Parameters:
e
- the database access exception- Returns:
- a generic message indicating database operation failure
-