From e0312a3359d7d2e7fe3d7771ad8d896133a2e2fc Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Mon, 12 Dec 2016 20:58:56 +0700 Subject: error page --- .../com/juick/www/controllers/ErrorController.java | 49 ++++++++++++++++++++++ .../www/controllers/ShowMessageController.java | 23 +++++----- .../src/main/resources/errors.properties | 5 +-- .../src/main/resources/errors_en.properties | 5 +-- .../webapp/WEB-INF/templates/layout/error.html | 25 +++++++++++ .../webapp/WEB-INF/templates/layout/login.html | 2 +- .../webapp/WEB-INF/templates/postNotFound.html | 23 ---------- .../webapp/WEB-INF/templates/userNotFound.html | 22 ---------- 8 files changed, 89 insertions(+), 65 deletions(-) create mode 100644 juick-spring-www/src/main/java/com/juick/www/controllers/ErrorController.java create mode 100644 juick-spring-www/src/main/webapp/WEB-INF/templates/layout/error.html delete mode 100644 juick-spring-www/src/main/webapp/WEB-INF/templates/postNotFound.html delete mode 100644 juick-spring-www/src/main/webapp/WEB-INF/templates/userNotFound.html diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/ErrorController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/ErrorController.java new file mode 100644 index 00000000..a3e697b4 --- /dev/null +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/ErrorController.java @@ -0,0 +1,49 @@ +package com.juick.www.controllers; + +import com.juick.server.util.HttpBadRequestException; +import com.juick.server.util.HttpForbiddenException; +import com.juick.server.util.HttpNotFoundException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; + +/** + * Created by aalexeev on 12/12/16. + */ +@ControllerAdvice +public class ErrorController { + private static Logger logger = LoggerFactory.getLogger(ErrorController.class); + + @ExceptionHandler(HttpBadRequestException.class) + @ResponseStatus(value = HttpStatus.BAD_REQUEST) + public String badRequest() { + return "layout/error"; + } + + @ExceptionHandler(HttpForbiddenException.class) + @ResponseStatus(value = HttpStatus.FORBIDDEN) + public String forbidden() { + return "layout/error"; + } + + @ExceptionHandler(HttpNotFoundException.class) + @ResponseStatus(value = HttpStatus.NOT_FOUND) + public String notFound() { + return "layout/error"; + } + + @ExceptionHandler(Throwable.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public String exception(final Throwable throwable, final Model model) { + logger.error("Exception during execution of SpringSecurity application", throwable); + + String errorMessage = (throwable != null ? throwable.getMessage() : "Unknown error"); + model.addAttribute("errorMessage", errorMessage); + + return "layout/error"; + } +} diff --git a/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java b/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java index e95bd7cf..df204c5f 100644 --- a/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java +++ b/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java @@ -1,6 +1,7 @@ package com.juick.www.controllers; import com.juick.User; +import com.juick.server.util.HttpNotFoundException; import com.juick.service.MessagesService; import com.juick.service.UserService; import com.juick.util.WebUtils; @@ -58,7 +59,7 @@ public class ShowMessageController { model.addAttribute("messageId", anything); - return "postNotFound"; + throw new HttpNotFoundException(); } } return showUserMessages(anything, null, before, model); @@ -88,7 +89,7 @@ public class ShowMessageController { User user = userService.getUserByName(userName); if (user == null) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } return "index"; @@ -101,13 +102,13 @@ public class ShowMessageController { // Check validity of user name before quering from database if (WebUtils.isNotUserName(userName)) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } User user = userService.getUserByName(userName); if (user == null) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } return "index"; @@ -120,13 +121,13 @@ public class ShowMessageController { // Check validity of user name before quering from database if (WebUtils.isNotUserName(userName)) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } User user = userService.getUserByName(userName); if (user == null) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } return "index"; @@ -139,13 +140,13 @@ public class ShowMessageController { // Check validity of user name before quering from database if (WebUtils.isNotUserName(userName)) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } User user = userService.getUserByName(userName); if (user == null) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } return "index"; @@ -159,19 +160,19 @@ public class ShowMessageController { // Check validity of post number before quering from database if (WebUtils.isNotPostNumber(postNumber)) { model.addAttribute("messageId", postNumber); - return "postNotFound"; + throw new HttpNotFoundException(); } // Check validity of user name before quering from database if (WebUtils.isNotUserName(userName)) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } User user = userService.getUserByName(userName); if (user == null) { model.addAttribute("userName", userName); - return "userNotFound"; + throw new HttpNotFoundException(); } diff --git a/juick-spring-www/src/main/resources/errors.properties b/juick-spring-www/src/main/resources/errors.properties index e000394e..ca13b926 100644 --- a/juick-spring-www/src/main/resources/errors.properties +++ b/juick-spring-www/src/main/resources/errors.properties @@ -1,6 +1,3 @@ -error.pageNotFound = Страница не найдена -errors.pageNotFound.extended = Похоже, пользователь удалил пост #{0}, или пост не был создан. -error.userNotFound = Пользователь не найден -errors.userNotFound.extended = Пользователь {0} не найден +error.title = Произошла ошибка error.login=Произошла ошибка, проверьте имя пользователя и пароль \ No newline at end of file diff --git a/juick-spring-www/src/main/resources/errors_en.properties b/juick-spring-www/src/main/resources/errors_en.properties index 7824bbe1..7ec8fbfd 100644 --- a/juick-spring-www/src/main/resources/errors_en.properties +++ b/juick-spring-www/src/main/resources/errors_en.properties @@ -1,6 +1,3 @@ -error.pageNotFound = Page not found -errors.pageNotFound.extended = Probably, user deleted this post #{0}, or this post never existed. -error.userNotFound = User not found -errors.userNotFound.extended = User {0} not found +error.title = Error page error.login=Wrong user or password \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/error.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/error.html new file mode 100644 index 00000000..60df54a7 --- /dev/null +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/error.html @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + Error page + + + +

404

+

Error java.lang.NullPointerException

+ Back to Home Page + + \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/login.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/login.html index 9fd43192..bcfa318d 100644 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/login.html +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/login.html @@ -131,9 +131,9 @@
+

Произошла ошибка, проверьте имя пользователя и пароль

Уже зарегистрированы?
-

Произошла ошибка, проверьте имя пользователя и пароль

diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/postNotFound.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/postNotFound.html deleted file mode 100644 index 4162aa8b..00000000 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/postNotFound.html +++ /dev/null @@ -1,23 +0,0 @@ - - - - Страница не найдена - - - -
-
-

Страница не найдена

-
- -
-

Похоже, пользователь удалил страницу, возможна она и не была создана.

-
-
- Вернуться на главную -
-
- - \ No newline at end of file diff --git a/juick-spring-www/src/main/webapp/WEB-INF/templates/userNotFound.html b/juick-spring-www/src/main/webapp/WEB-INF/templates/userNotFound.html deleted file mode 100644 index de1a97a0..00000000 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/userNotFound.html +++ /dev/null @@ -1,22 +0,0 @@ - - - - Пользователь не найден - - -
-
-

Пользователь не найден

-
- -
-

Пользователь не найден.

-
-
- Вернуться на главную -
-
- - \ No newline at end of file -- cgit v1.2.3