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

Sprint-15 [Yurii Leskiv] Implemented error handling #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.softserve.itacademy.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.persistence.EntityNotFoundException;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(Exception.class)
public ModelAndView handleServerException (Exception e, HttpServletRequest request) {
ModelAndView mav = new ModelAndView();
mav.setViewName("500-page");
mav.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
mav.addObject("message", "Message: " + e.getMessage());
mav.addObject("timestamp", "Date: " +LocalDateTime.now());
mav.addObject("path", "Path: " + request.getRequestURI());
return mav;
}

@ExceptionHandler({EntityNotFoundException.class, NullEntityReferenceException.class})
public ModelAndView handleEntityNotFoundException(RuntimeException e, HttpServletRequest request) {
ModelAndView mav = new ModelAndView();
mav.setViewName("404-page");
mav.setStatus(e instanceof EntityNotFoundException ? HttpStatus.NOT_FOUND : HttpStatus.BAD_REQUEST);
mav.addObject("message", "Message: " + e.getMessage());
mav.addObject("error", "Error: " + (e instanceof EntityNotFoundException ? "Not Found" :"Bad Request"));
mav.addObject("timestamp", "Date: " +LocalDateTime.now());
mav.addObject("path", "Path: " + request.getRequestURI());
return mav;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.softserve.itacademy.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.BAD_REQUEST)
public class NullEntityReferenceException extends RuntimeException {

public NullEntityReferenceException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@

@Repository
public interface RoleRepository extends JpaRepository<Role, Long> {

Role removeById(long id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface StateRepository extends JpaRepository<State, Long> {

@Query(value = "select * from states order by id", nativeQuery = true)
List<State> getAll();

State removeById(long id);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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 org.springframework.stereotype.Service;

import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -20,25 +22,35 @@ public RoleServiceImpl(RoleRepository roleRepository) {

@Override
public Role create(Role role) {
return roleRepository.save(role);
return Optional
.ofNullable(role)
.map(roleRepository::save)
.orElseThrow(() -> new NullEntityReferenceException("Role can't be null and won't be affected!"));
}

@Override
public Role readById(long id) {
Optional<Role> optional = roleRepository.findById(id);
return optional.get();
return Optional
.of(id)
.flatMap(roleRepository::findById)
.orElseThrow(() -> new EntityNotFoundException("Role with id " + id + " not found"));
}

@Override
public Role update(Role role) {
Role oldRole = readById(role.getId());
return roleRepository.save(role);
return Optional
.ofNullable(role)
.map(roleRepository::save)
.orElseThrow(() -> new NullEntityReferenceException("Role can't be null and won't be updated"));
}

@Override
public void delete(long id) {
Role role = readById(id);
roleRepository.delete(role);
Optional
.of(id)
.filter(roleRepository::existsById)
.map(roleRepository::removeById)
.orElseThrow(() -> new EntityNotFoundException("Role with id " + id + " not found"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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;
Expand All @@ -19,25 +21,35 @@ public StateServiceImpl(StateRepository stateRepository) {

@Override
public State create(State state) {
return stateRepository.save(state);
return Optional
.ofNullable(state)
.map(stateRepository::save)
.orElseThrow(() -> new NullEntityReferenceException("State can't be null and won't be affected!"));
}

@Override
public State readById(long id) {
Optional<State> optional = stateRepository.findById(id);
return optional.get();
return Optional
.of(id)
.flatMap(stateRepository::findById)
.orElseThrow(() -> new EntityNotFoundException("State with id " + id + " not found"));
}

@Override
public State update(State state) {
State oldState = readById(state.getId());
return stateRepository.save(state);
return Optional
.ofNullable(state)
.map(stateRepository::save)
.orElseThrow(() -> new NullEntityReferenceException("State can't be null and won't be updated"));
}

@Override
public void delete(long id) {
State state = readById(id);
stateRepository.delete(state);
Optional
.of(id)
.filter(stateRepository::existsById)
.map(stateRepository::removeById)
.orElseThrow(() -> new EntityNotFoundException("State with id " + id + " not found"));
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions src/main/resources/templates/404-page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>404-Error</title>
</head>
<body>
<h2 th:text="${error}"></h2>
<h2 th:text="${message}"></h2>
<h2 th:text="${timestamp}"></h2>
<h2 th:text="'Status: ' + ${#response.getStatus()}"></h2>
<h2 th:text="${path}"></h2>
</body>
</html>
14 changes: 14 additions & 0 deletions src/main/resources/templates/500-page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>Error: Internal Server Error</h2>
<h2>Status: 500</h2>
<h2 th:text="${message}"></h2>
<h2 th:text="${timestamp}"></h2>
<h2 th:text="${path}"></h2>
</body>
</html>