Class GlobalExceptionHandler

java.lang.Object
com.fiap.tech_challenge.parte1.ms_users.exceptions.GlobalExceptionHandler

@ControllerAdvice public class GlobalExceptionHandler extends Object
Global exception handler to centralize and customize exception handling across all controllers in the application.

It intercepts specific exceptions and builds detailed error responses with HTTP status, timestamps, error messages, and request details to be sent back to clients.

  • Constructor Details

    • GlobalExceptionHandler

      public GlobalExceptionHandler()
  • Method Details

    • handleUserNotFoundException

      @ExceptionHandler(UserNotFoundException.class) @ResponseStatus(NOT_FOUND) public org.springframework.http.ResponseEntity<Map<String,Object>> handleUserNotFoundException(UserNotFoundException ex, jakarta.servlet.http.HttpServletRequest request)
      Handles exceptions when a user resource is not found.
      Parameters:
      ex - the exception thrown when the user is not found
      request - the current HTTP request
      Returns:
      a ResponseEntity with status 404 and error details in the body
    • handleTypeMismatch

      @ExceptionHandler(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException.class) @ResponseStatus(BAD_REQUEST) public org.springframework.http.ResponseEntity<Object> handleTypeMismatch(jakarta.servlet.http.HttpServletRequest request)
      Handles exceptions caused by invalid type for method arguments, for example when a path variable or request parameter cannot be converted to the expected type.
      Parameters:
      request - the current HTTP request
      Returns:
      a ResponseEntity with status 400 and a generic invalid parameter message
    • handleValidationExceptions

      @ExceptionHandler(org.springframework.web.bind.MethodArgumentNotValidException.class) @ResponseStatus(BAD_REQUEST) public org.springframework.http.ResponseEntity<Object> handleValidationExceptions(org.springframework.web.bind.MethodArgumentNotValidException ex, jakarta.servlet.http.HttpServletRequest request)
      Handles exceptions triggered when method arguments fail validation, such as @Valid annotated DTOs with invalid fields.
      Parameters:
      ex - the MethodArgumentNotValidException containing validation errors
      request - the current HTTP request
      Returns:
      a ResponseEntity with status 400 containing field-specific validation errors
    • handleEmailAlreadyExists

      @ExceptionHandler(EmailAlreadyExistsException.class) @ResponseStatus(CONFLICT) public org.springframework.http.ResponseEntity<Object> handleEmailAlreadyExists(EmailAlreadyExistsException ex, jakarta.servlet.http.HttpServletRequest request)
      Handles exceptions when an email already exists in the system.
      Parameters:
      ex - the EmailAlreadyExistsException
      request - the current HTTP request
      Returns:
      a ResponseEntity with status 409 and the exception message
    • handleLoginAlreadyExists

      @ExceptionHandler(LoginAlreadyExistsException.class) @ResponseStatus(CONFLICT) public org.springframework.http.ResponseEntity<Object> handleLoginAlreadyExists(LoginAlreadyExistsException ex, jakarta.servlet.http.HttpServletRequest request)
      Handles exceptions when a login already exists in the system.
      Parameters:
      ex - the LoginAlreadyExistsException
      request - the current HTTP request
      Returns:
      a ResponseEntity with status 409 and the exception message
    • handleDuplicatedAddress

      @ExceptionHandler(DuplicatedAddressException.class) @ResponseStatus(CONFLICT) public org.springframework.http.ResponseEntity<Object> handleDuplicatedAddress(DuplicatedAddressException ex, jakarta.servlet.http.HttpServletRequest request)
      Handles exceptions when a duplicate address is detected.
      Parameters:
      ex - the DuplicatedAddressException
      request - the current HTTP request
      Returns:
      a ResponseEntity with status 409 and the exception message
    • handleMissingParams

      @ExceptionHandler(org.springframework.web.bind.MissingServletRequestParameterException.class) @ResponseStatus(BAD_REQUEST) public org.springframework.http.ResponseEntity<Object> handleMissingParams(org.springframework.web.bind.MissingServletRequestParameterException ex, jakarta.servlet.http.HttpServletRequest request)
      Handles exceptions triggered when a required request parameter is missing.
      Parameters:
      ex - the MissingServletRequestParameterException
      request - the current HTTP request
      Returns:
      a ResponseEntity with status 400 and a message identifying the missing parameter
    • handleInvalidPasswordException

      @ExceptionHandler(InvalidPasswordException.class) @ResponseStatus(BAD_REQUEST) public org.springframework.http.ResponseEntity<Object> handleInvalidPasswordException(InvalidPasswordException ex, jakarta.servlet.http.HttpServletRequest request)
      Handles exceptions when a password is invalid according to business rules.
      Parameters:
      ex - the InvalidPasswordException
      request - the current HTTP request
      Returns:
      a ResponseEntity with status 400 and the exception message