Class GlobalExceptionHandler

java.lang.Object
me.josephsf.jobportaljosephsfeir.exception.GlobalExceptionHandler

@RestControllerAdvice public class GlobalExceptionHandler extends Object
Global exception handler that provides centralized exception handling across all controllers in the Job Portal application.

This class uses the RestControllerAdvice annotation to capture exceptions thrown from any controller in the application and convert them into standardized HTTP responses with appropriate status codes and structured error messages.

The handler manages various types of exceptions:

  • Custom application exceptions like ResourceNotFoundException
  • Security-related exceptions from Spring Security
  • Validation failures from Bean Validation
  • Generic and unexpected exceptions

Each exception type is mapped to an appropriate HTTP status code and formatted into a consistent error response structure to provide clear error information to API clients.

Since:
2025-05-15
  • Constructor Details

    • GlobalExceptionHandler

      public GlobalExceptionHandler()
      Constructs a new GlobalExceptionHandler instance.

      This default constructor initializes the exception handler with no specific configuration as it relies on Spring's annotation-based exception handling.

  • Method Details

    • handleResourceNotFoundException

      @ExceptionHandler(ResourceNotFoundException.class) public ResponseEntity<me.josephsf.jobportaljosephsfeir.exception.GlobalExceptionHandler.ErrorResponse> handleResourceNotFoundException(ResourceNotFoundException ex, WebRequest request)
      Handles ResourceNotFoundException, returning a 404 NOT_FOUND response.
      Parameters:
      ex - the exception that was thrown
      request - the current request
      Returns:
      a ResponseEntity containing error details and 404 status code
    • handleBadRequestException

      @ExceptionHandler(BadRequestException.class) public ResponseEntity<me.josephsf.jobportaljosephsfeir.exception.GlobalExceptionHandler.ErrorResponse> handleBadRequestException(BadRequestException ex, WebRequest request)
      Handles BadRequestException, returning a 400 BAD_REQUEST response.
      Parameters:
      ex - the exception that was thrown
      request - the current request
      Returns:
      a ResponseEntity containing error details and 400 status code
    • handleUnauthorizedException

      @ExceptionHandler(UnauthorizedException.class) public ResponseEntity<me.josephsf.jobportaljosephsfeir.exception.GlobalExceptionHandler.ErrorResponse> handleUnauthorizedException(UnauthorizedException ex, WebRequest request)
      Handles UnauthorizedException, returning a 401 UNAUTHORIZED response.
      Parameters:
      ex - the exception that was thrown
      request - the current request
      Returns:
      a ResponseEntity containing error details and 401 status code
    • handleAccessDeniedException

      @ExceptionHandler(org.springframework.security.access.AccessDeniedException.class) public ResponseEntity<me.josephsf.jobportaljosephsfeir.exception.GlobalExceptionHandler.ErrorResponse> handleAccessDeniedException(org.springframework.security.access.AccessDeniedException ex, WebRequest request)
      Handles Spring Security's AccessDeniedException, returning a 403 FORBIDDEN response.
      Parameters:
      ex - the exception that was thrown
      request - the current request
      Returns:
      a ResponseEntity containing error details and 403 status code
    • handleBadCredentialsException

      @ExceptionHandler(org.springframework.security.authentication.BadCredentialsException.class) public ResponseEntity<ApiResponseDto> handleBadCredentialsException(org.springframework.security.authentication.BadCredentialsException ex)
      Handles Spring Security's BadCredentialsException, returning a 401 UNAUTHORIZED response. Used for invalid login attempts.
      Parameters:
      ex - the exception that was thrown
      Returns:
      a ResponseEntity containing error details and 401 status code
    • handleValidationExceptions

      Handles validation failures from @Valid annotations on request bodies, returning a 400 BAD_REQUEST response with details of each validation failure.
      Parameters:
      ex - the exception that was thrown
      Returns:
      a ResponseEntity containing validation error details and 400 status code
    • handleGlobalException

      @ExceptionHandler(java.lang.Exception.class) public ResponseEntity<me.josephsf.jobportaljosephsfeir.exception.GlobalExceptionHandler.ErrorResponse> handleGlobalException(Exception ex, WebRequest request)
      Fallback handler for any unhandled exceptions, returning a 500 INTERNAL_SERVER_ERROR response.
      Parameters:
      ex - the exception that was thrown
      request - the current request
      Returns:
      a ResponseEntity containing error details and 500 status code
    • handleIllegalArgumentException

      @ExceptionHandler(java.lang.IllegalArgumentException.class) public ResponseEntity<me.josephsf.jobportaljosephsfeir.exception.GlobalExceptionHandler.ErrorResponse> handleIllegalArgumentException(IllegalArgumentException ex, WebRequest request)
      Handles IllegalArgumentException, returning a 400 BAD_REQUEST response.
      Parameters:
      ex - the exception that was thrown
      request - the current request
      Returns:
      a ResponseEntity containing error details and 400 status code