Class GlobalExceptionHandler
java.lang.Object
com.equipmentrental.equipment_rental_system.controller.GlobalExceptionHandler
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionhandleBookingConflict(BookingConflictException exception, org.springframework.ui.Model model) Handles booking date conflicts (BR-02).handleDeletionBlocked(DeletionBlockedException exception, org.springframework.ui.Model model) Handles attempts to delete entities with dependent records (BR-06, BR-07).handleEquipmentNotAvailable(EquipmentNotAvailableException exception, org.springframework.ui.Model model) Handles attempts to book equipment that is not available (BR-01).handleGenericException(Exception exception, org.springframework.ui.Model model) Catches any unhandled exception, including missing static resources (favicon.ico).handleInvalidBookingDates(InvalidBookingDatesException exception, org.springframework.ui.Model model) Handles invalid booking date ranges (BR-05).handleResourceNotFound(ResourceNotFoundException exception, org.springframework.ui.Model model) Handles cases where a requested entity was not found by its ID.
-
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 IDmodel- 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 conflictmodel- 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 equipmentmodel- 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 datesmodel- 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 blockedmodel- 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 exceptionmodel- the model to populate with error attributes- Returns:
- the error view name
-