From 2bb84da3fd5510696741b9ae0cc5316067fb4c69 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Wed, 3 Jul 2024 15:09:52 +0000 Subject: [PATCH 01/19] add deadline --- README.md | 1 + 1 file changed, 1 insertion(+) 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. From 068926d2d5948ec45b2234a954651b6a0c8d120b Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 12:18:06 +0300 Subject: [PATCH 02/19] 404 page creation --- src/main/resources/templates/404.html | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/resources/templates/404.html diff --git a/src/main/resources/templates/404.html b/src/main/resources/templates/404.html new file mode 100644 index 0000000..9f05a33 --- /dev/null +++ b/src/main/resources/templates/404.html @@ -0,0 +1,19 @@ + + + + + + + + + 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 From 75704ec00d61190ffc05b4945dfba24cd45cf8f2 Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 12:42:56 +0300 Subject: [PATCH 03/19] modification for 404 page --- .../softserve/itacademy/service/impl/RoleServiceImpl.java | 8 +++----- src/main/resources/templates/404.html | 7 ++++++- 2 files changed, 9 insertions(+), 6 deletions(-) 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..c2c39d3 100644 --- a/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java @@ -3,6 +3,7 @@ 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 java.util.ArrayList; @@ -10,13 +11,10 @@ import java.util.Optional; @Service +@RequiredArgsConstructor public class RoleServiceImpl implements RoleService { - private RoleRepository roleRepository; - - public RoleServiceImpl(RoleRepository roleRepository) { - this.roleRepository = roleRepository; - } + private final RoleRepository roleRepository; @Override public Role create(Role role) { diff --git a/src/main/resources/templates/404.html b/src/main/resources/templates/404.html index 9f05a33..c69e80e 100644 --- a/src/main/resources/templates/404.html +++ b/src/main/resources/templates/404.html @@ -12,7 +12,12 @@

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.

+
+

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
From 5eb565ecbe1aba4a159e33ea030fa1c444ea5934 Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 12:54:07 +0300 Subject: [PATCH 04/19] modification for 404 page --- src/main/resources/templates/404.html | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/resources/templates/404.html b/src/main/resources/templates/404.html index c69e80e..a358193 100644 --- a/src/main/resources/templates/404.html +++ b/src/main/resources/templates/404.html @@ -8,6 +8,11 @@ Page Not Found +
From c1ce0570057145dee73404a7af85fe1da04da808 Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 13:02:48 +0300 Subject: [PATCH 05/19] modification for 404 page --- src/main/resources/templates/404.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/templates/404.html b/src/main/resources/templates/404.html index a358193..e67b843 100644 --- a/src/main/resources/templates/404.html +++ b/src/main/resources/templates/404.html @@ -10,7 +10,7 @@ Page Not Found From e673145af0312d0b09d9c256c69cc505f199a0b2 Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 13:19:28 +0300 Subject: [PATCH 06/19] new 400 page --- src/main/resources/templates/400.html | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/resources/templates/400.html diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html new file mode 100644 index 0000000..30db404 --- /dev/null +++ b/src/main/resources/templates/400.html @@ -0,0 +1,19 @@ + + + + + + + 400 Bad Request + + +

400 Bad Request

+

+ +

+
    +
  • +
+ + \ No newline at end of file From 695951755c09bdf417a61fbf3f400407c4acadfa Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 16:52:59 +0300 Subject: [PATCH 07/19] new 400 page --- src/main/resources/templates/400.html | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html index 30db404..274cc99 100644 --- a/src/main/resources/templates/400.html +++ b/src/main/resources/templates/400.html @@ -8,12 +8,12 @@ 400 Bad Request -

400 Bad Request

-

- -

-
    -
  • -
+

400 Bad Request

+

+ +

+
    +
  • +
\ No newline at end of file From 0c9e56839d85b6c1fe5e4b2e3a78c4ea7e84c199 Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 16:55:55 +0300 Subject: [PATCH 08/19] updated 400 page --- src/main/resources/templates/400.html | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html index 274cc99..f5e1e7e 100644 --- a/src/main/resources/templates/400.html +++ b/src/main/resources/templates/400.html @@ -8,12 +8,15 @@ 400 Bad Request -

400 Bad Request

-

- -

-
    -
  • -
+
+
+

400 Bad Request

+ + +

+ +

+
+
\ No newline at end of file From ea055a0eb483a1b14b0cd762a77d21c3a4c78c23 Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 16:56:35 +0300 Subject: [PATCH 09/19] updated 400 page --- src/main/resources/templates/400.html | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html index f5e1e7e..da259dd 100644 --- a/src/main/resources/templates/400.html +++ b/src/main/resources/templates/400.html @@ -12,7 +12,6 @@

400 Bad Request

-

From 87c91c2ca0ad9693b76d7908babafe693ee4949c Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Thu, 4 Jul 2024 21:32:23 +0300 Subject: [PATCH 10/19] updated 400 page --- src/main/resources/templates/400.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html index da259dd..76140ac 100644 --- a/src/main/resources/templates/400.html +++ b/src/main/resources/templates/400.html @@ -12,8 +12,8 @@

400 Bad Request

-

- +

+

From 77426a96e3a49dd50a4d7f9bc03098c1eed8b7a8 Mon Sep 17 00:00:00 2001 From: BogdanPhoenix Date: Fri, 5 Jul 2024 11:34:29 +0300 Subject: [PATCH 11/19] Implementation of the GlobalExceptionHandler. --- .../exception/GlobalExceptionHandler.java | 50 +++++++++++++++++++ .../NullEntityReferenceException.java | 4 ++ 2 files changed, 54 insertions(+) create mode 100644 src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java create mode 100644 src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java 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..60fe305 --- /dev/null +++ b/src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java @@ -0,0 +1,50 @@ +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"; + } + } + +} 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..21cb2f3 --- /dev/null +++ b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java @@ -0,0 +1,4 @@ +package com.softserve.itacademy.exception; + +public class NullEntityReferenceException { +} From e3b1c2577d4d6ce1e56c0dd976eafbd9a7ac508d Mon Sep 17 00:00:00 2001 From: BogdanPhoenix Date: Fri, 5 Jul 2024 11:41:32 +0300 Subject: [PATCH 12/19] Adding exceptions to the StateService. --- .../itacademy/service/StateService.java | 14 ++++--- .../service/impl/StateServiceImpl.java | 42 ++++++++++++------- 2 files changed, 37 insertions(+), 19 deletions(-) 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/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; } + } From 78fa7b9f8bbab0684d1f64e8c517af1386700a21 Mon Sep 17 00:00:00 2001 From: BogdanPhoenix Date: Fri, 5 Jul 2024 11:41:51 +0300 Subject: [PATCH 13/19] Adding exceptions to the TaskService. --- .../itacademy/service/TaskService.java | 12 ++++--- .../service/impl/TaskServiceImpl.java | 35 +++++++++++++------ 2 files changed, 32 insertions(+), 15 deletions(-) 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/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 From c08c1bb65a5075734e7051c3b4c98002dc8c6b6e Mon Sep 17 00:00:00 2001 From: Vadym Honcharuk Date: Fri, 5 Jul 2024 13:42:26 +0300 Subject: [PATCH 14/19] add NullEntityReferenceException --- .../exception/NullEntityReferenceException.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java 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..ca114cc --- /dev/null +++ b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java @@ -0,0 +1,11 @@ +package com.softserve.itacademy.exception; + +public class NullEntityReferenceException extends Exception { + public NullEntityReferenceException() { + super("Reference to an entity is null"); + } + + public NullEntityReferenceException(String message) { + super(message); + } +} From 9d12b9cc320f1d7704c56375d39d4af64aecf209 Mon Sep 17 00:00:00 2001 From: Vadym Honcharuk Date: Fri, 5 Jul 2024 14:10:15 +0300 Subject: [PATCH 15/19] do some changes in NullEntityReferenceException --- .../itacademy/exception/NullEntityReferenceException.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java index ca114cc..7fd1f9f 100644 --- a/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java +++ b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java @@ -1,8 +1,8 @@ package com.softserve.itacademy.exception; -public class NullEntityReferenceException extends Exception { +public class NullEntityReferenceException extends RuntimeException { public NullEntityReferenceException() { - super("Reference to an entity is null"); + super(); } public NullEntityReferenceException(String message) { From ccb1288f3f494e02468db192d9ec78377821ddc3 Mon Sep 17 00:00:00 2001 From: Vadym Honcharuk Date: Fri, 5 Jul 2024 14:11:03 +0300 Subject: [PATCH 16/19] add handle exception in methods for UserServiceImpl --- .../service/impl/UserServiceImpl.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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); } From 6064e200a2d5876d5dac70da2aa6cd506946192e Mon Sep 17 00:00:00 2001 From: SonnyRose Date: Fri, 5 Jul 2024 15:31:33 +0300 Subject: [PATCH 17/19] realization of RoleService --- .../exception/GlobalExceptionHandler.java | 49 +++++++++++++++++++ .../NullEntityReferenceException.java | 11 +++++ .../itacademy/service/RoleService.java | 10 ++-- .../service/impl/RoleServiceImpl.java | 33 +++++++++---- 4 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/softserve/itacademy/exception/GlobalExceptionHandler.java create mode 100644 src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java 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..a89db1a --- /dev/null +++ b/src/main/java/com/softserve/itacademy/exception/NullEntityReferenceException.java @@ -0,0 +1,11 @@ +package com.softserve.itacademy.exception; + +public class NullEntityReferenceException extends RuntimeException{ + public NullEntityReferenceException() { + super(); + } + + public NullEntityReferenceException(String message) { + super(message); + } +} 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/impl/RoleServiceImpl.java b/src/main/java/com/softserve/itacademy/service/impl/RoleServiceImpl.java index c2c39d3..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,40 +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 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 "; + 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); } From 27f74c7cf7fdeb677a61d486da520c4e36c7c8e7 Mon Sep 17 00:00:00 2001 From: Dneprik Date: Sat, 6 Jul 2024 01:14:58 +0200 Subject: [PATCH 18/19] 500 + ToDoService --- .../itacademy/service/ToDoService.java | 11 +++++---- .../service/impl/ToDoServiceImpl.java | 17 +++++++++++--- src/main/resources/templates/500.html | 23 +++++++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 src/main/resources/templates/500.html 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/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/resources/templates/500.html b/src/main/resources/templates/500.html new file mode 100644 index 0000000..6648e1d --- /dev/null +++ b/src/main/resources/templates/500.html @@ -0,0 +1,23 @@ + + + + + + + + 500 Internal Server Error + + +
+

500 Internal Server Error

+
+

Sorry, something went wrong on the server. You will be redirected to the home page in 5 seconds.

+
+
+

+
+ Go to Home page +
+ + \ No newline at end of file From e5bf239ac5603df5d570f686cbe325b2dd30211d Mon Sep 17 00:00:00 2001 From: Dneprik Date: Sat, 6 Jul 2024 01:27:10 +0200 Subject: [PATCH 19/19] edit 500 --- src/main/resources/templates/500.html | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/resources/templates/500.html b/src/main/resources/templates/500.html index 6648e1d..05dba3b 100644 --- a/src/main/resources/templates/500.html +++ b/src/main/resources/templates/500.html @@ -2,22 +2,20 @@ - 500 Internal Server Error - -
-

500 Internal Server Error

-
-

Sorry, something went wrong on the server. You will be redirected to the home page in 5 seconds.

+ +
+
+

500 Internal Server Error

+ +

+ +

-
-

-
- Go to Home page
\ No newline at end of file