Class GlobalExceptionHandler

java.lang.Object
com.alpaca.exception.GlobalExceptionHandler

@RestControllerAdvice public class GlobalExceptionHandler extends Object
Centralized exception handler for REST controllers using RestControllerAdvice.

This class intercepts exceptions thrown by controller methods and converts them into meaningful HTTP responses with structured payloads.

Handled exception types include:

  • SpecificException: returns a status code and reason from the exception.
  • MethodArgumentNotValidException: captures validation errors and returns a map of field names to error messages.
  • Generic Exception: wraps unexpected errors in an ErrorResponseDTO with timestamp and request description, returning HTTP 500.
  • ResponseStatusException: translates the exception into an error response using the exception’s status and reason.

This approach centralizes error logic, improves API consistency, and cleanly separates concern across controllers. It reflects recommended practices for production-grade REST APIs using Spring Boot. RestControllerAdvice automatically applies to all controllers globally.

See Also:
  • RestControllerAdvice
  • ExceptionHandler
  • Constructor Details

    • GlobalExceptionHandler

      public GlobalExceptionHandler()
  • Method Details

    • handleSpecificException

      @ExceptionHandler(SpecificException.class) public org.springframework.http.ResponseEntity<String> handleSpecificException(SpecificException specificException)
      Handles custom SpecificException and returns the configured status and reason.
      Parameters:
      specificException - the thrown specific exception
      Returns:
      a ResponseEntity containing the reason and matching status code
    • handleMethodArgumentNotValidException

      @ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) public org.springframework.http.ResponseEntity<HashMap<String,String>> handleMethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException exception)
      Captures validation errors thrown due to method argument validation failures. Constructs a field-to-error message map for easy client-side display.
      Parameters:
      exception - the validation exception containing field errors
      Returns:
      a BAD_REQUEST response containing a map of field-specific error messages
    • handleGlobalException

      @ExceptionHandler(java.lang.Exception.class) public org.springframework.http.ResponseEntity<ErrorResponseDTO> handleGlobalException(Exception exception, org.springframework.web.context.request.WebRequest webRequest)
      Catches all unhandled exceptions and returns a structured error response with details including the request path, error message, and timestamp.
      Parameters:
      exception - the uncaught exception
      webRequest - the current web request to extract context
      Returns:
      an INTERNAL_SERVER_ERROR response with an ErrorResponseDTO
    • handleResponseStatusException

      @ExceptionHandler(org.springframework.web.server.ResponseStatusException.class) public org.springframework.http.ResponseEntity<ErrorResponseDTO> handleResponseStatusException(org.springframework.web.server.ResponseStatusException exception, org.springframework.web.context.request.WebRequest webRequest)
      Handles ResponseStatusException by passing through its status and reason into a structured error payload.
      Parameters:
      exception - the exception containing a predefined HTTP status
      webRequest - the current web request for context
      Returns:
      response with the appropriate HTTP status and an ErrorResponseDTO