diff --git a/README.md b/README.md index 40c3372..5fa0a49 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/fcC7yEpn) # Java Online Marathon ## To-Do List Project 1. Implement exception handling for custom exception. diff --git a/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java b/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..f1db39a --- /dev/null +++ b/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java @@ -0,0 +1,49 @@ +package com.softserve.itacademy.exception; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.servlet.ModelAndView; + +import javax.persistence.EntityNotFoundException; +import javax.servlet.http.HttpServletRequest; + +@Slf4j +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(NullEntityReferenceException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public ModelAndView handleNullEntityReferenceException(HttpServletRequest request, NullEntityReferenceException ex) { + return createModelAndView(request, HttpStatus.BAD_REQUEST, ex); + } + + @ExceptionHandler(EntityNotFoundException.class) + @ResponseStatus(HttpStatus.NOT_FOUND) + public ModelAndView handleEntityNotFoundException(HttpServletRequest request, EntityNotFoundException ex) { + return createModelAndView(request, HttpStatus.NOT_FOUND, ex); + } + + @ExceptionHandler(Exception.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public ModelAndView handleException(HttpServletRequest request, Exception ex) { + return createModelAndView(request, HttpStatus.INTERNAL_SERVER_ERROR, ex); + } + + private static ModelAndView createModelAndView(HttpServletRequest request, HttpStatus httpStatus, Exception ex) { + log.error("An error occurred: \"{}\" at the specified URL: \"{}\".", ex.getMessage(), request.getRequestURI()); + ModelAndView modelAndView = new ModelAndView(getView(httpStatus)); + modelAndView.addObject("errorMessage", ex.getMessage()); + return modelAndView; + } + + private static String getView(HttpStatus httpStatus) { + switch (httpStatus) { + case BAD_REQUEST: return "400"; + case NOT_FOUND: return "404"; + default: return "500"; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java new file mode 100644 index 0000000..046d99b --- /dev/null +++ b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java @@ -0,0 +1,13 @@ +package com.softserve.itacademy.exception; + + +public class NullEntityReferenceException extends RuntimeException { + + public NullEntityReferenceException() { + super(); + } + + public NullEntityReferenceException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/com/softserve/itacademy/service/RoleService.java b/src/main/java/com/softserve/itacademy/service/RoleService.java index 6b44062..68ef711 100644 --- a/src/main/java/com/softserve/itacademy/service/RoleService.java +++ b/src/main/java/com/softserve/itacademy/service/RoleService.java @@ -1,13 +1,15 @@ package com.softserve.itacademy.service; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.Role; +import javax.persistence.EntityNotFoundException; import java.util.List; public interface RoleService { - Role create(Role role); - Role readById(long id); - Role update(Role role); - void delete(long id); + Role create(Role role) throws NullEntityReferenceException; + Role readById(long id) throws EntityNotFoundException; + Role update(Role role) throws NullEntityReferenceException, EntityNotFoundException; + void delete(long id) throws EntityNotFoundException; List getAll(); } diff --git a/src/main/java/com/softserve/itacademy/service/StateService.java b/src/main/java/com/softserve/itacademy/service/StateService.java index 1449fb7..d23cae7 100644 --- a/src/main/java/com/softserve/itacademy/service/StateService.java +++ b/src/main/java/com/softserve/itacademy/service/StateService.java @@ -1,15 +1,19 @@ package com.softserve.itacademy.service; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.State; +import javax.persistence.EntityNotFoundException; import java.util.List; public interface StateService { - State create(State state); - State readById(long id); - State update(State state); - void delete(long id); - State getByName(String name); + State create(State state) throws NullEntityReferenceException; + State readById(long id) throws EntityNotFoundException; + State update(State state) throws NullEntityReferenceException, EntityNotFoundException; + void delete(long id) throws EntityNotFoundException; + + State getByName(String name) throws EntityNotFoundException; List getAll(); + } diff --git a/src/main/java/com/softserve/itacademy/service/TaskService.java b/src/main/java/com/softserve/itacademy/service/TaskService.java index b6e4318..74538f8 100644 --- a/src/main/java/com/softserve/itacademy/service/TaskService.java +++ b/src/main/java/com/softserve/itacademy/service/TaskService.java @@ -1,15 +1,19 @@ package com.softserve.itacademy.service; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.Task; +import javax.persistence.EntityNotFoundException; import java.util.List; public interface TaskService { - Task create(Task task); - Task readById(long id); - Task update(Task task); - void delete(long id); + + Task create(Task task) throws NullEntityReferenceException; + Task readById(long id) throws EntityNotFoundException; + Task update(Task task) throws NullEntityReferenceException, EntityNotFoundException; + void delete(long id) throws EntityNotFoundException; List getAll(); List getByTodoId(long todoId); + } diff --git a/src/main/java/com/softserve/itacademy/service/ToDoService.java b/src/main/java/com/softserve/itacademy/service/ToDoService.java index 5c57637..d786ca1 100644 --- a/src/main/java/com/softserve/itacademy/service/ToDoService.java +++ b/src/main/java/com/softserve/itacademy/service/ToDoService.java @@ -1,14 +1,17 @@ package com.softserve.itacademy.service; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.ToDo; +import javax.persistence.EntityNotFoundException; +import javax.validation.constraints.Null; import java.util.List; public interface ToDoService { - ToDo create(ToDo todo); - ToDo readById(long id); - ToDo update(ToDo todo); - void delete(long id); + ToDo create(ToDo todo) throws NullEntityReferenceException; + ToDo readById(long id) throws EntityNotFoundException; + ToDo update(ToDo todo) throws NullEntityReferenceException, EntityNotFoundException; + void delete(long id) throws EntityNotFoundException; List getAll(); List getByUserId(long userId); diff --git a/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java index 7d517c6..952522b 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java @@ -1,42 +1,53 @@ package com.softserve.itacademy.service.impl; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.Role; import com.softserve.itacademy.repository.RoleRepository; import com.softserve.itacademy.service.RoleService; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import javax.persistence.EntityNotFoundException; import java.util.ArrayList; import java.util.List; -import java.util.Optional; @Service +@RequiredArgsConstructor public class RoleServiceImpl implements RoleService { - private RoleRepository roleRepository; + private static final String ROLE_CANNOT_BE_NULL = "Role cannot be null"; + private static final String ROLE_NOT_FOUND = "Role not found"; + private static final String ROLE_NOT_FOUND_WITH_ID = "Role not found with id "; - public RoleServiceImpl(RoleRepository roleRepository) { - this.roleRepository = roleRepository; - } + private final RoleRepository roleRepository; @Override - public Role create(Role role) { - return roleRepository.save(role); + public Role create(Role role) throws NullEntityReferenceException { + if (role == null){ + throw new NullEntityReferenceException(ROLE_CANNOT_BE_NULL); + } + return roleRepository.save(role); } @Override - public Role readById(long id) { - Optional optional = roleRepository.findById(id); - return optional.get(); + public Role readById(long id) throws EntityNotFoundException { + return roleRepository.findById(id) + .orElseThrow(() -> new EntityNotFoundException(ROLE_NOT_FOUND)); } @Override - public Role update(Role role) { - Role oldRole = readById(role.getId()); - return roleRepository.save(role); + public Role update(Role role) throws EntityNotFoundException, NullEntityReferenceException { + if (role == null){ + throw new NullEntityReferenceException(ROLE_CANNOT_BE_NULL); + } + if (!roleRepository.existsById(role.getId())){ + throw new EntityNotFoundException(ROLE_NOT_FOUND_WITH_ID + role.getId()); + } + return roleRepository.save(role); } @Override - public void delete(long id) { + public void delete(long id) throws EntityNotFoundException { Role role = readById(id); roleRepository.delete(role); } diff --git a/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java index ac95e84..cd4026a 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/StateServiceImpl.java @@ -1,49 +1,62 @@ package com.softserve.itacademy.service.impl; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.State; import com.softserve.itacademy.repository.StateRepository; import com.softserve.itacademy.service.StateService; import org.springframework.stereotype.Service; +import javax.persistence.EntityNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Optional; @Service public class StateServiceImpl implements StateService { - private StateRepository stateRepository; + + private static final String NULL_ENTITY_EXCEPTION_MESSAGE = "The passed State object cannot be null."; + + private final StateRepository stateRepository; public StateServiceImpl(StateRepository stateRepository) { this.stateRepository = stateRepository; } @Override - public State create(State state) { - return stateRepository.save(state); + public State create(State state) throws NullEntityReferenceException { + if(state == null) { + throw new NullEntityReferenceException(NULL_ENTITY_EXCEPTION_MESSAGE); + } + + return stateRepository.save(state); } @Override - public State readById(long id) { - Optional optional = stateRepository.findById(id); - return optional.get(); + public State readById(long id) throws EntityNotFoundException { + return stateRepository.findById(id) + .orElseThrow(() -> new EntityNotFoundException(String.format("An object of class State could not be found by the specified identifier \"%d\".", id))); } @Override - public State update(State state) { - State oldState = readById(state.getId()); - return stateRepository.save(state); + public State update(State state) throws NullEntityReferenceException, EntityNotFoundException { + if(state == null) { + throw new NullEntityReferenceException(NULL_ENTITY_EXCEPTION_MESSAGE); + } + + readById(state.getId()); + return stateRepository.save(state); } @Override - public void delete(long id) { + public void delete(long id) throws EntityNotFoundException { State state = readById(id); - stateRepository.delete(state); + stateRepository.delete(state); } @Override - public State getByName(String name) { - Optional optional = Optional.ofNullable(stateRepository.getByName(name)); - return optional.get(); + public State getByName(String name) throws EntityNotFoundException { + return Optional.ofNullable(stateRepository.getByName(name)) + .orElseThrow(() -> new EntityNotFoundException(String.format("An object of class State could not be found by the specified name \"%s\".", name))); } @Override @@ -51,4 +64,5 @@ public List getAll() { List states = stateRepository.getAll(); return states.isEmpty() ? new ArrayList<>() : states; } + } diff --git a/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java index 6161dc8..a40561f 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/TaskServiceImpl.java @@ -1,43 +1,56 @@ package com.softserve.itacademy.service.impl; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.Task; import com.softserve.itacademy.repository.TaskRepository; import com.softserve.itacademy.service.TaskService; import org.springframework.stereotype.Service; +import javax.persistence.EntityNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Optional; @Service public class TaskServiceImpl implements TaskService { - private TaskRepository taskRepository; + + private static final String NULL_ENTITY_EXCEPTION_MESSAGE = "The passed Task object cannot be null."; + + private final TaskRepository taskRepository; public TaskServiceImpl(TaskRepository taskRepository) { this.taskRepository = taskRepository; } @Override - public Task create(Task user) { - return taskRepository.save(user); + public Task create(Task user) throws NullEntityReferenceException { + if(user == null) { + throw new NullEntityReferenceException(NULL_ENTITY_EXCEPTION_MESSAGE); + } + + return taskRepository.save(user); } @Override - public Task readById(long id) { - Optional optional = taskRepository.findById(id); - return optional.get(); + public Task readById(long id) throws EntityNotFoundException { + return taskRepository.findById(id) + .orElseThrow(() -> new EntityNotFoundException(String.format("An object of class Task could not be found by the specified identifier \"%d\".", id))); } @Override - public Task update(Task task) { - Task oldTask = readById(task.getId()); - return taskRepository.save(task); + public Task update(Task task) throws NullEntityReferenceException, EntityNotFoundException { + if(task == null) { + throw new NullEntityReferenceException(NULL_ENTITY_EXCEPTION_MESSAGE); + } + + readById(task.getId()); + return taskRepository.save(task); } @Override - public void delete(long id) { + public void delete(long id) throws EntityNotFoundException { Task task = readById(id); - taskRepository.delete(task); + taskRepository.delete(task); } @Override diff --git a/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java index d000571..d29705b 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/ToDoServiceImpl.java @@ -1,10 +1,13 @@ package com.softserve.itacademy.service.impl; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.ToDo; import com.softserve.itacademy.repository.ToDoRepository; import com.softserve.itacademy.service.ToDoService; import org.springframework.stereotype.Service; +import javax.persistence.EntityNotFoundException; +import javax.validation.constraints.Null; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -20,19 +23,27 @@ public ToDoServiceImpl(ToDoRepository todoRepository) { @Override public ToDo create(ToDo todo) { + if (todo == null) { + throw new NullEntityReferenceException("ToDo cannot be null"); + } return todoRepository.save(todo); } @Override public ToDo readById(long id) { Optional optional = todoRepository.findById(id); - return optional.get(); + return optional.orElseThrow(() -> new EntityNotFoundException("ToDO not found with id " + id)); } @Override public ToDo update(ToDo todo) { - ToDo oldTodo = readById(todo.getId()); - return todoRepository.save(todo); + if (todo == null) { + throw new NullEntityReferenceException("ToDo cannot be null"); + } + if (!todoRepository.existsById(todo.getId())) { + throw new EntityNotFoundException("ToDo not found with id " + todo.getId()); + } + return todoRepository.save(todo); } @Override diff --git a/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java index 5b3645f..a966c37 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/UserServiceImpl.java @@ -1,10 +1,12 @@ package com.softserve.itacademy.service.impl; +import com.softserve.itacademy.exception.NullEntityReferenceException; import com.softserve.itacademy.model.User; import com.softserve.itacademy.repository.UserRepository; import com.softserve.itacademy.service.UserService; import org.springframework.stereotype.Service; +import javax.persistence.EntityNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -12,6 +14,9 @@ @Service public class UserServiceImpl implements UserService { + private static final String NULL_ENTITY_EXCEPTION = "Reference to an entity is null"; + private static final String ENTITY_NOT_FOUND_EXCEPTION = "Reference to an entity is not found"; + private UserRepository userRepository; public UserServiceImpl(UserRepository userRepository) { @@ -20,23 +25,36 @@ public UserServiceImpl(UserRepository userRepository) { @Override public User create(User user) { + if(user == null){ + throw new NullEntityReferenceException(NULL_ENTITY_EXCEPTION); + } + return userRepository.save(user); } @Override public User readById(long id) { + if(!userRepository.existsById(id)){ + throw new EntityNotFoundException(ENTITY_NOT_FOUND_EXCEPTION); + } Optional optional = userRepository.findById(id); return optional.get(); } @Override public User update(User user) { + if(user == null){ + throw new NullEntityReferenceException(NULL_ENTITY_EXCEPTION); + } User oldUser = readById(user.getId()); return userRepository.save(user); } @Override public void delete(long id) { + if(!userRepository.existsById(id)){ + throw new EntityNotFoundException(ENTITY_NOT_FOUND_EXCEPTION); + } User user = readById(id); userRepository.delete(user); } diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html new file mode 100644 index 0000000..76140ac --- /dev/null +++ b/src/main/resources/templates/400.html @@ -0,0 +1,21 @@ + + + + + + + 400 Bad Request + + +
+
+

400 Bad Request

+ +

+ +

+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/404.html b/src/main/resources/templates/404.html new file mode 100644 index 0000000..e67b843 --- /dev/null +++ b/src/main/resources/templates/404.html @@ -0,0 +1,29 @@ + + + + + + + + + Page Not Found + + + +
+

404 Page Not Found

+
+

Sorry, the page you are looking for does not exist. You will be redirected to the home page in 5 seconds.

+
+
+

+
+ Go to Home page +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/500.html b/src/main/resources/templates/500.html new file mode 100644 index 0000000..05dba3b --- /dev/null +++ b/src/main/resources/templates/500.html @@ -0,0 +1,21 @@ + + + + + + + 500 Internal Server Error + + +
+
+

500 Internal Server Error

+ +

+ +

+
+
+ + \ No newline at end of file