From 728fa4297278be50a17d55e2036d828cc940b27a Mon Sep 17 00:00:00 2001 From: "Artem.Arefev" Date: Fri, 17 May 2019 02:01:26 +0400 Subject: [PATCH] 87 error handling --- ...ntrollerV2.java => UserMvcController.java} | 19 +++++++++++------ .../ru/ulstu/user/service/UserService.java | 5 ++++- src/main/resources/templates/error/error.html | 21 +++++++++++++++++++ .../resources/templates/users/profile.html | 9 ++++++++ 4 files changed, 47 insertions(+), 7 deletions(-) rename src/main/java/ru/ulstu/user/controller/{UserControllerV2.java => UserMvcController.java} (77%) create mode 100644 src/main/resources/templates/error/error.html diff --git a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java b/src/main/java/ru/ulstu/user/controller/UserMvcController.java similarity index 77% rename from src/main/java/ru/ulstu/user/controller/UserControllerV2.java rename to src/main/java/ru/ulstu/user/controller/UserMvcController.java index eee98a1..c35dd79 100644 --- a/src/main/java/ru/ulstu/user/controller/UserControllerV2.java +++ b/src/main/java/ru/ulstu/user/controller/UserMvcController.java @@ -3,14 +3,15 @@ package ru.ulstu.user.controller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; 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.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.configuration.Constants; import ru.ulstu.odin.controller.OdinController; -import ru.ulstu.user.error.UserSendingMailException; import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserListDto; @@ -22,20 +23,26 @@ import javax.servlet.http.HttpSession; @Controller @RequestMapping(value = "/users") -public class UserControllerV2 extends OdinController { +public class UserMvcController extends OdinController { - private final Logger log = LoggerFactory.getLogger(UserControllerV2.class); + private final Logger log = LoggerFactory.getLogger(UserMvcController.class); private final UserService userService; private final UserSessionService userSessionService; - public UserControllerV2(UserService userService, - UserSessionService userSessionService) { + public UserMvcController(UserService userService, + UserSessionService userSessionService) { super(UserListDto.class, UserDto.class); this.userService = userService; this.userSessionService = userSessionService; } + @ExceptionHandler(Exception.class) + public String errorHandler(Model model, Exception exception) { + model.addAttribute("error", exception.getMessage()); + return "/error/error"; + } + @GetMapping("/profile") public void getUserProfile(ModelMap modelMap, HttpServletRequest request) { HttpSession session = request.getSession(false); @@ -52,7 +59,7 @@ public class UserControllerV2 extends OdinController { } @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); return "redirect:/"; } diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index 083b0f5..78f9d66 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -52,6 +52,8 @@ import java.util.stream.Collectors; @Service @Transactional public class UserService implements UserDetailsService { + private static final String INVITE_USER_EXCEPTION = "Во время отправки приглашения произошла ошибка"; + private final Logger log = LoggerFactory.getLogger(UserService.class); private final UserRepository userRepository; private final PasswordEncoder passwordEncoder; @@ -213,6 +215,7 @@ public class UserService implements UserDetailsService { user.setFirstName(updateUser.getFirstName()); user.setLastName(updateUser.getLastName()); user.setEmail(updateUser.getEmail()); + user.setLogin(updateUser.getLogin()); user = userRepository.save(user); log.debug("Updated Information for User: {}", user.getLogin()); return userMapper.userEntityToUserDto(user); @@ -340,7 +343,7 @@ public class UserService implements UserDetailsService { try { mailService.sendInviteMail(variables, email); } catch (MessagingException e) { - throw new UserSendingMailException(email); + throw new UserSendingMailException(INVITE_USER_EXCEPTION); } } } diff --git a/src/main/resources/templates/error/error.html b/src/main/resources/templates/error/error.html new file mode 100644 index 0000000..53c9054 --- /dev/null +++ b/src/main/resources/templates/error/error.html @@ -0,0 +1,21 @@ + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/users/profile.html b/src/main/resources/templates/users/profile.html index c97e539..93adfc7 100644 --- a/src/main/resources/templates/users/profile.html +++ b/src/main/resources/templates/users/profile.html @@ -49,6 +49,15 @@ class="alert alert-danger">Incorrect email

+
+ + +

Incorrect email

+

+