content: package com.activecourses.upwork.exception;
  
  import com.activecourses.upwork.dto.ResponseDto;
  import org.springframework.dao.DataIntegrityViolationException;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.security.authentication.BadCredentialsException;
  import org.springframework.web.bind.MethodArgumentNotValidException;
  import org.springframework.web.bind.annotation.ControllerAdvice;
  import org.springframework.web.bind.annotation.ExceptionHandler;
  
  import java.util.ArrayList;
  import java.util.List;
  
  @ControllerAdvice
  public class GlobalExceptionHandler {
  
      @ExceptionHandler(DataIntegrityViolationException.class)
      public ResponseEntity<String> handleDataIntegrityViolation(DataIntegrityViolationException e) {
          return new ResponseEntity<>("Email already exists", HttpStatus.CONFLICT);
      }
  
      @ExceptionHandler(MethodArgumentNotValidException.class)
      public ResponseEntity<?> handleValidationExceptions(MethodArgumentNotValidException ex) {
  
          List<String> errors = new ArrayList<>();
          ex.getBindingResult().getAllErrors().forEach((error) -> {
              String errorMessage = error.getDefaultMessage();
              errors.add(errorMessage);
          });
          return ResponseEntity
                  .status(HttpStatus.BAD_REQUEST)
                  .body(ResponseDto
                          .builder()
                          .status(HttpStatus.BAD_REQUEST)
                          .success(false)
                          .error(errors)
                          .build()
                  );
  
      }
  
      @ExceptionHandler(AuthenticationException.class)
      public ResponseEntity<ResponseDto> handleAuthenticationException(AuthenticationException ex) {
          return ResponseEntity
                  .status(HttpStatus.UNAUTHORIZED)
                  .body(ResponseDto
                          .builder()
                          .status(HttpStatus.UNAUTHORIZED)
                          .success(false)
                          .error(ex.getMessage())
                          .build()
                  );
      }
  
      @ExceptionHandler(BadCredentialsException.class)
      public ResponseEntity<ResponseDto> handleBadCredentialsException(BadCredentialsException ex) {
          return ResponseEntity
                  .status(HttpStatus.UNAUTHORIZED)
                  .body(ResponseDto
                          .builder()
                          .status(HttpStatus.UNAUTHORIZED)
                          .success(false)
                          .error("Invalid username or password")
                          .build()
                  );
      }
  }
  