Class GlobalExceptionHandler

java.lang.Object
com.equipmentrental.equipment_rental_system.controller.GlobalExceptionHandler

@ControllerAdvice public class GlobalExceptionHandler extends Object
Centralised exception handler for all controllers in the application. Uses Spring's ControllerAdvice to intercept exceptions thrown by any controller and route them to the error page with an appropriate status code and user-friendly message.

Each custom exception type has a dedicated handler method. A catch-all handler covers unexpected exceptions to ensure users never see a raw stack trace.

See Also:
  • Constructor Details

    • GlobalExceptionHandler

      public GlobalExceptionHandler()
  • Method Details

    • handleResourceNotFound

      @ExceptionHandler(ResourceNotFoundException.class) public String handleResourceNotFound(ResourceNotFoundException exception, org.springframework.ui.Model model)
      Handles cases where a requested entity was not found by its ID.
      Parameters:
      exception - the exception containing the resource name and ID
      model - the model to populate with error attributes
      Returns:
      the error view name
    • handleBookingConflict

      @ExceptionHandler(BookingConflictException.class) public String handleBookingConflict(BookingConflictException exception, org.springframework.ui.Model model)
      Handles booking date conflicts (BR-02).
      Parameters:
      exception - the exception describing the conflict
      model - the model to populate with error attributes
      Returns:
      the error view name
    • handleEquipmentNotAvailable

      @ExceptionHandler(EquipmentNotAvailableException.class) public String handleEquipmentNotAvailable(EquipmentNotAvailableException exception, org.springframework.ui.Model model)
      Handles attempts to book equipment that is not available (BR-01).
      Parameters:
      exception - the exception describing the unavailable equipment
      model - the model to populate with error attributes
      Returns:
      the error view name
    • handleInvalidBookingDates

      @ExceptionHandler(InvalidBookingDatesException.class) public String handleInvalidBookingDates(InvalidBookingDatesException exception, org.springframework.ui.Model model)
      Handles invalid booking date ranges (BR-05).
      Parameters:
      exception - the exception describing the invalid dates
      model - the model to populate with error attributes
      Returns:
      the error view name
    • handleDeletionBlocked

      @ExceptionHandler(DeletionBlockedException.class) public String handleDeletionBlocked(DeletionBlockedException exception, org.springframework.ui.Model model)
      Handles attempts to delete entities with dependent records (BR-06, BR-07).
      Parameters:
      exception - the exception explaining why deletion was blocked
      model - the model to populate with error attributes
      Returns:
      the error view name
    • handleGenericException

      @ExceptionHandler(Exception.class) public String handleGenericException(Exception exception, org.springframework.ui.Model model)
      Catches any unhandled exception, including missing static resources (favicon.ico). Logs the full stack trace for unexpected errors and returns a generic error page.
      Parameters:
      exception - the unhandled exception
      model - the model to populate with error attributes
      Returns:
      the error view name