Fix type of throws exceptions on methods

This commit is contained in:
rayankonecny 2025-12-18 04:53:14 +00:00
parent b83d0b3faf
commit ff96ecf8a1
5 changed files with 38 additions and 17 deletions

View file

@ -3,34 +3,41 @@ package br.com.rayankonecny.authserviceapi.controllers.exceptions;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import jakarta.servlet.http.HttpServletRequest;
import br.com.rayankonecny.hdcommoslib.models.exceptions.RefreshTokenExpired;
import br.com.rayankonecny.hdcommoslib.models.exceptions.StandardError;
import br.com.rayankonecny.hdcommoslib.models.exceptions.ValidationException;
import static java.time.LocalDateTime.now;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import java.util.ArrayList;
@ControllerAdvice
public class ControllerExceptionHandler {
@ExceptionHandler(UsernameNotFoundException.class)
ResponseEntity<StandardError> handleNotFoundException(final UsernameNotFoundException ex,final HttpServletRequest request) {
// @ExceptionHandler(UsernameNotFoundException.class)
// ResponseEntity<StandardError> handleNotAuthorizedException(final UsernameNotFoundException ex,final HttpServletRequest request) {
return ResponseEntity.status(NOT_FOUND).body(
StandardError.builder()
.timestamp(now())
.status(NOT_FOUND.value())
.error(NOT_FOUND.getReasonPhrase())
.message(ex.getMessage())
.path(request.getRequestURI())
.build());
}
// return ResponseEntity.status(UNAUTHORIZED).body(
// StandardError.builder()
// .timestamp(now())
// .status(UNAUTHORIZED.value())
// .error(UNAUTHORIZED.getReasonPhrase())
// .message(ex.getMessage())
// .path(request.getRequestURI())
// .build());
// }
@ExceptionHandler({BadCredentialsException.class,RefreshTokenExpired.class})
ResponseEntity<StandardError> handleBadCredentialsException(final BadCredentialsException ex, final HttpServletRequest request) {
@ExceptionHandler({BadCredentialsException.class,RefreshTokenExpired.class,UsernameNotFoundException.class})
ResponseEntity<StandardError> handleBadCredentialsException(final RuntimeException ex, final HttpServletRequest request) {
return ResponseEntity.status(UNAUTHORIZED).body(
StandardError.builder()
@ -41,4 +48,18 @@ public class ControllerExceptionHandler {
.path(request.getRequestURI())
.build());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
ResponseEntity<StandardError> handleMethodArgumentNotValidException(final MethodArgumentNotValidException ex,
final HttpServletRequest request) {
var error = ValidationException.builder().timestamp(now()).status(BAD_REQUEST.value()).error("ValidationException")
.message("Exception in validation attributes").path(request.getRequestURI()).errors(new ArrayList<>()).build();
for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
error.addError(fieldError.getField(), fieldError.getDefaultMessage());
}
return ResponseEntity.badRequest().body(error);
}
}

View file

@ -25,7 +25,7 @@ public class AuthService {
var user = userDetailsService.loadUserByUsername(request.email());
if (!passwordEncoder.matches(request.password(), user.getPassword())) {
throw new BadCredentialsException("Invalid credentials");
throw new BadCredentialsException("Email or password invalid");
}
String token = jwtUtils.generateToken(user);

View file

@ -21,7 +21,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
public UserDetailsDTO loadUserByUsername(final String email) throws UsernameNotFoundException {
final var entity = repository.findByEmail(email)
.orElseThrow(() -> new UsernameNotFoundException("User not found: " + email));
.orElseThrow(() -> new UsernameNotFoundException("Email or password invalid"));
return UserDetailsDTO.builder()
.id(entity.getId())

View file

@ -9,4 +9,4 @@ spring:
enabled: false
jwt.secret: "IHf3Yua/byvtA+iIcGWmkrLvpKEXTb5ClkXaZ0VDmYbr/6b1otCs38x68bidvZLAOB7anUtVQlCid6YDULO5XA=="
jwt.expiration: 120000
jwt.expiration-sec.refresh-token: 3600
jwt.expiration-sec.refresh-token: 5