Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom exception handling mechanism is added #572

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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