Skip to content

Commit

Permalink
Merge pull request #128 from evrentan/fix/add-user-already-exists-exc…
Browse files Browse the repository at this point in the history
…eption

fix(ApplicationUserServiceImpl): add UserAlreadyExistsException
  • Loading branch information
evrentan authored Feb 4, 2024
2 parents cd09374 + feb3fc8 commit b39e8d2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public ResponseEntity<CustomRestError> notFoundException(final Exception excepti
* @since 1.0.0
*/
@ExceptionHandler({
UserTypeAlreadyExistsException.class
UserTypeAlreadyExistsException.class,
UserAlreadyExistsException.class
})
public ResponseEntity<CustomRestError> alreadyExistsException(final Exception exception, final HttpServletRequest httpServletRequest) {
var customRestError = CustomRestError.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package evrentan.community.usermanager.exception;

public class UserAlreadyExistsException extends RuntimeException {

private final String message;

public UserAlreadyExistsException(String message) {
this.message = message;
}

@Override
public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import evrentan.community.usermanager.dto.entity.ApplicationUser;
import evrentan.community.usermanager.entity.ApplicationUserEntity;
import evrentan.community.usermanager.exception.IdNotMatchException;
import evrentan.community.usermanager.exception.UserAlreadyExistsException;
import evrentan.community.usermanager.exception.UserNotFoundException;
import evrentan.community.usermanager.mapper.ApplicationUserMapper;
import evrentan.community.usermanager.message.ExceptionMessages;
Expand All @@ -11,7 +12,6 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.UUID;

Expand Down Expand Up @@ -43,7 +43,7 @@ public ApplicationUserServiceImpl(ApplicationUserRepository applicationUserRepos
@Override
public ApplicationUser createUser(ApplicationUser applicationUser) {
if (this.applicationUserRepository.existsDistinctByEmail(applicationUser.getEmail()))
throw new IllegalArgumentException(USER_ALREADY_EXIST);
throw new UserAlreadyExistsException(ExceptionMessages.USER_ALREADY_EXISTS);

return ApplicationUserMapper.toDto(this.applicationUserRepository.save(ApplicationUserMapper.toEntity(applicationUser)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class ExceptionMessages {

public static final String USER_NOT_FOUND = "User does not exist !!!";
public static final String USER_ALREADY_EXIST = "User already exists !!!";
public static final String USER_ALREADY_EXISTS = "User already exists !!!";
public static final String USER_TYPE_NOT_FOUND = "User Type does not exist !!!";
public static final String USER_TYPE_ALREADY_EXIST = "User Type already exists !!!";
public static final String ID_NOT_MATCH = "Id does not match !!!";
Expand Down

0 comments on commit b39e8d2

Please sign in to comment.