87 error handling

This commit is contained in:
Artem.Arefev 2019-05-17 02:01:26 +04:00
parent d50673f5fa
commit 728fa42972
4 changed files with 47 additions and 7 deletions

View File

@ -3,14 +3,15 @@ package ru.ulstu.user.controller;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.configuration.Constants; import ru.ulstu.configuration.Constants;
import ru.ulstu.odin.controller.OdinController; import ru.ulstu.odin.controller.OdinController;
import ru.ulstu.user.error.UserSendingMailException;
import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserDto;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import ru.ulstu.user.model.UserListDto; import ru.ulstu.user.model.UserListDto;
@ -22,20 +23,26 @@ import javax.servlet.http.HttpSession;
@Controller @Controller
@RequestMapping(value = "/users") @RequestMapping(value = "/users")
public class UserControllerV2 extends OdinController<UserListDto, UserDto> { public class UserMvcController extends OdinController<UserListDto, UserDto> {
private final Logger log = LoggerFactory.getLogger(UserControllerV2.class); private final Logger log = LoggerFactory.getLogger(UserMvcController.class);
private final UserService userService; private final UserService userService;
private final UserSessionService userSessionService; private final UserSessionService userSessionService;
public UserControllerV2(UserService userService, public UserMvcController(UserService userService,
UserSessionService userSessionService) { UserSessionService userSessionService) {
super(UserListDto.class, UserDto.class); super(UserListDto.class, UserDto.class);
this.userService = userService; this.userService = userService;
this.userSessionService = userSessionService; this.userSessionService = userSessionService;
} }
@ExceptionHandler(Exception.class)
public String errorHandler(Model model, Exception exception) {
model.addAttribute("error", exception.getMessage());
return "/error/error";
}
@GetMapping("/profile") @GetMapping("/profile")
public void getUserProfile(ModelMap modelMap, HttpServletRequest request) { public void getUserProfile(ModelMap modelMap, HttpServletRequest request) {
HttpSession session = request.getSession(false); HttpSession session = request.getSession(false);
@ -52,7 +59,7 @@ public class UserControllerV2 extends OdinController<UserListDto, UserDto> {
} }
@PostMapping("/invite") @PostMapping("/invite")
public String inviteUser(@RequestParam(value = "email") String email) throws UserSendingMailException { public String inviteUser(@RequestParam(value = "email") String email, ModelMap modelMap) {
userService.inviteUser(email); userService.inviteUser(email);
return "redirect:/"; return "redirect:/";
} }

View File

@ -52,6 +52,8 @@ import java.util.stream.Collectors;
@Service @Service
@Transactional @Transactional
public class UserService implements UserDetailsService { public class UserService implements UserDetailsService {
private static final String INVITE_USER_EXCEPTION = "Во время отправки приглашения произошла ошибка";
private final Logger log = LoggerFactory.getLogger(UserService.class); private final Logger log = LoggerFactory.getLogger(UserService.class);
private final UserRepository userRepository; private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder; private final PasswordEncoder passwordEncoder;
@ -213,6 +215,7 @@ public class UserService implements UserDetailsService {
user.setFirstName(updateUser.getFirstName()); user.setFirstName(updateUser.getFirstName());
user.setLastName(updateUser.getLastName()); user.setLastName(updateUser.getLastName());
user.setEmail(updateUser.getEmail()); user.setEmail(updateUser.getEmail());
user.setLogin(updateUser.getLogin());
user = userRepository.save(user); user = userRepository.save(user);
log.debug("Updated Information for User: {}", user.getLogin()); log.debug("Updated Information for User: {}", user.getLogin());
return userMapper.userEntityToUserDto(user); return userMapper.userEntityToUserDto(user);
@ -340,7 +343,7 @@ public class UserService implements UserDetailsService {
try { try {
mailService.sendInviteMail(variables, email); mailService.sendInviteMail(variables, email);
} catch (MessagingException e) { } catch (MessagingException e) {
throw new UserSendingMailException(email); throw new UserSendingMailException(INVITE_USER_EXCEPTION);
} }
} }
} }

View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<div class="container" layout:fragment="content">
<section id="services">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading text-uppercase"><span th:text="${error}"></span></h2>
<a href="/"><h3>Вернуться на главную</h3></a>
</div>
</div>
</div>
</section>
</div>
</body>
</html>

View File

@ -49,6 +49,15 @@
class="alert alert-danger">Incorrect email</p> class="alert alert-danger">Incorrect email</p>
<p class="help-block text-danger"></p> <p class="help-block text-danger"></p>
</div> </div>
<div class="form-group">
<label for="login">Логин:</label>
<input class="form-control" id="login" type="text"
placeholder="Login"
th:field="*{login}"/>
<p th:if="${#fields.hasErrors('login')}" th:errors="*{login}"
class="alert alert-danger">Incorrect email</p>
<p class="help-block text-danger"></p>
</div>
<div class="form-group"> <div class="form-group">
<button id="sendMessageButton" name="save" <button id="sendMessageButton" name="save"
class="btn btn-success text-uppercase" class="btn btn-success text-uppercase"