Skip to content

Commit

Permalink
feat(exception): segregate community manager exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
evrentan committed Feb 3, 2024
1 parent 4169180 commit d9ae216
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package evrentan.community.communitymanager.exception;

public class CommunityNotFoundException extends RuntimeException {

private final String message;

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

@Override
public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class GlobalRestExceptionHandler {
@ExceptionHandler(BadRequestException.class)
public ResponseEntity<CustomRestError> badRequestException(final Exception exception, final HttpServletRequest httpServletRequest){
var customRestError = CustomRestError.builder()
.message(exception.getCause().getMessage())
.message(exception.getMessage())
.status(HttpStatus.BAD_REQUEST.value())
.build();
return responseEntity(customRestError);
Expand All @@ -51,10 +51,13 @@ public ResponseEntity<CustomRestError> badRequestException(final Exception excep
* @author <a href="https://github.com/Onuraktasj">Onur Aktas</a>
* @since 1.0.0
*/
@ExceptionHandler(NotFoundException.class)
@ExceptionHandler({
NotFoundException.class,
CommunityNotFoundException.class
})
public ResponseEntity<CustomRestError> notFoundException(final Exception exception, final HttpServletRequest httpServletRequest){
var customRestError = CustomRestError.builder()
.message(exception.getCause().getMessage())
.message(exception.getMessage())
.status(HttpStatus.NOT_FOUND.value())
.build();
return responseEntity(customRestError);
Expand All @@ -74,7 +77,7 @@ public ResponseEntity<CustomRestError> notFoundException(final Exception excepti
@ExceptionHandler(InternalServerErrorException.class)
public ResponseEntity<CustomRestError> ınternalServerException(final Exception exception,final HttpServletRequest httpServletRequest){
var customRestError = CustomRestError.builder()
.message(exception.getCause().getMessage())
.message(exception.getMessage())
.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
.build();
return responseEntity(customRestError);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package evrentan.community.communitymanager.impl;

import evrentan.community.communitymanager.dto.entity.Community;
import evrentan.community.communitymanager.exception.CommunityNotFoundException;
import evrentan.community.communitymanager.entity.CommunityEntity;
import evrentan.community.communitymanager.mapper.CommunityMapper;
import evrentan.community.communitymanager.message.ExceptionMessages;
import evrentan.community.communitymanager.repository.CommunityRepository;
import evrentan.community.communitymanager.service.CommunityService;
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 @@ -51,7 +52,12 @@ public Community createCommunity(Community community) {
*/
@Override
public List<Community> getCommunities() {
return CommunityMapper.toDtoList(this.communityRepository.findAll());
List<Community> communityList = CommunityMapper.toDtoList(this.communityRepository.findAll());

if (communityList.isEmpty())
throw new CommunityNotFoundException(ExceptionMessages.COMMUNITY_NOT_FOUND);

return communityList;
}

/**
Expand All @@ -65,7 +71,7 @@ public List<Community> getCommunities() {
*/
@Override
public Community getCommunity(UUID id) {
return CommunityMapper.toDto(this.communityRepository.findById(id).orElseThrow(() -> new NoSuchElementException("Community not found")));
return CommunityMapper.toDto(this.communityRepository.findById(id).orElseThrow(() -> new CommunityNotFoundException(ExceptionMessages.COMMUNITY_NOT_FOUND)));
}

/**
Expand Down Expand Up @@ -95,11 +101,11 @@ public Boolean checkCommunityStatusById(UUID id) {
*/
@Override
public Community updateCommunity(UUID id, Community community) {
if(!Objects.equals(id, community.getId()))
if (!Objects.equals(id, community.getId()))
throw new IllegalArgumentException("Ids do not match");

if (!this.communityRepository.existsById(id))
throw new NoSuchElementException("Community not found");
throw new CommunityNotFoundException(ExceptionMessages.COMMUNITY_NOT_FOUND);

return CommunityMapper.toDto(this.communityRepository.save(CommunityMapper.toEntity(community)));
}
Expand All @@ -115,7 +121,7 @@ public Community updateCommunity(UUID id, Community community) {
*/
@Override
public Community updateCommunityStatus(UUID id, boolean status) {
CommunityEntity communityEntity = this.communityRepository.findById(id).orElseThrow(() -> new NoSuchElementException("Community not found"));
CommunityEntity communityEntity = this.communityRepository.findById(id).orElseThrow(() -> new CommunityNotFoundException(ExceptionMessages.COMMUNITY_NOT_FOUND));
communityEntity.setActive(status);

return CommunityMapper.toDto(this.communityRepository.save(communityEntity));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package evrentan.community.communitymanager.message;

public class ExceptionMessages {

public static final String COMMUNITY_NOT_FOUND = "Community not found!";
}

0 comments on commit d9ae216

Please sign in to comment.