Skip to content

Commit

Permalink
Merge pull request #572 from bounswe/backend/add-exception-handling
Browse files Browse the repository at this point in the history
custom exception handling mechanism is added
  • Loading branch information
zeynep-baydemir authored Oct 30, 2023
2 parents fec853b + 8365f9c commit f9e28e1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.app.gamereview.controller;


import com.app.gamereview.exception.BadRequestException;
import com.app.gamereview.exception.ResourceNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;


@ControllerAdvice
public class CustomExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<Object> handleResourceNotFoundException(ResourceNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ex.getMessage());
}

@ExceptionHandler(BadRequestException.class)
public ResponseEntity<Object> handleBadRequestException(BadRequestException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.app.gamereview.exception;

public class BadRequestException extends RuntimeException {
public BadRequestException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.app.gamereview.exception;

public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.app.gamereview.dto.request.*;
import com.app.gamereview.dto.response.LoginUserResponseDto;
import com.app.gamereview.dto.response.UserResponseDto;
import com.app.gamereview.exception.BadRequestException;
import com.app.gamereview.exception.ResourceNotFoundException;
import com.app.gamereview.model.User;
import com.app.gamereview.repository.UserRepository;
import org.modelmapper.ModelMapper;
Expand Down Expand Up @@ -33,8 +35,7 @@ public User registerUser(RegisterUserRequestDto registerUserRequestDto) {
Optional<User> sameEmail = userRepository.findByEmailAndIsDeletedFalse(registerUserRequestDto.getEmail());

if (sameUsername.isPresent() || sameEmail.isPresent()) {
// TODO : will add exception handling mechanism and custom exceptions
return null;
throw new BadRequestException("User with the same information already exists");
}
User userToCreate = modelMapper.map(registerUserRequestDto, User.class);
userToCreate.setIsDeleted(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.app.gamereview.dto.response.UserResponseDto;
import com.app.gamereview.dto.response.tag.AddGameTagResponseDto;
import com.app.gamereview.dto.response.tag.GetAllTagsOfGameResponseDto;
import com.app.gamereview.exception.ResourceNotFoundException;
import com.app.gamereview.model.Tag;
import com.app.gamereview.model.User;
import com.app.gamereview.repository.TagRepository;
Expand Down Expand Up @@ -94,8 +95,7 @@ public GetAllTagsOfGameResponseDto getGameTags(String gameId){
Optional<Game> findGame = gameRepository.findById(gameId);

if(findGame.isEmpty() || findGame.get().getIsDeleted()){
// TODO exception
return null;
throw new ResourceNotFoundException("Game does not exist");
}

Game game = findGame.get();
Expand All @@ -120,13 +120,11 @@ public AddGameTagResponseDto addGameTag(AddGameTagRequestDto request){
Optional<Tag> findTag = tagRepository.findById(request.getTagId());

if(findGame.isEmpty() || findGame.get().getIsDeleted()){
// TODO exception
return null;
throw new ResourceNotFoundException("Game does not exist");
}

if(findTag.isEmpty() || findTag.get().getIsDeleted()){
// TODO exception
return null;
throw new ResourceNotFoundException("Tag does not exist");
}

Game game = findGame.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.app.gamereview.dto.request.tag.CreateTagRequestDto;
import com.app.gamereview.dto.request.tag.GetAllTagsFilterRequestDto;
import com.app.gamereview.dto.request.tag.UpdateTagRequestDto;
import com.app.gamereview.exception.BadRequestException;
import com.app.gamereview.exception.ResourceNotFoundException;
import com.app.gamereview.model.Tag;
import com.app.gamereview.model.User;
import com.app.gamereview.repository.TagRepository;
Expand Down Expand Up @@ -68,8 +70,7 @@ public Tag createTag(CreateTagRequestDto request){
.findByNameAndIsDeletedFalse(request.getName());

if (sameName.isPresent()) {
// TODO : will add exception handling mechanism and custom exceptions
return null;
throw new BadRequestException("Tag with the same name already exist");
}
Tag tagToCreate = modelMapper.map(request, Tag.class);
tagToCreate.setIsDeleted(false);
Expand All @@ -81,8 +82,7 @@ public Tag updateTag(String id, UpdateTagRequestDto request){
Optional<Tag> tag = tagRepository.findById(id);

if (tag.isEmpty() || tag.get().getIsDeleted()){
// TODO : will add exception handling mechanism and custom exceptions
return null;
throw new ResourceNotFoundException("Tag desired to be updated does not exist");
}

Tag tagToUpdate = modelMapper.map(request, Tag.class);
Expand Down

0 comments on commit f9e28e1

Please sign in to comment.