diff options
Diffstat (limited to 'juick-spring-www/src')
8 files changed, 89 insertions, 65 deletions
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 000000000..a3e697b4d --- /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 e95bd7cf5..df204c5f1 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 e000394e3..ca13b9269 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 7824bbe1f..7ec8fbfd2 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 000000000..60df54a78 --- /dev/null +++ b/juick-spring-www/src/main/webapp/WEB-INF/templates/layout/error.html @@ -0,0 +1,25 @@ +<!DOCTYPE html> +<html xmlns:th="http://www.thymeleaf.org"> +<head> + <meta charset="UTF-8" /> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/> + + <link rel="stylesheet" href="/style.css" th:href="@{/style.css}"/> + + <script type="text/javascript" src="/scripts.js" th:href="@{/scripts.js}"></script> + + <link rel="icon" href="//i.juick.com/favicon.png"/> + <!--[if lt IE 9 & (!IEMobile 7)]> + <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script> + <![endif]--> + + <title th:text="#{error.title}">Error page</title> +</head> + +<body th:with="httpStatus=${T(org.springframework.http.HttpStatus).valueOf(#response.status)}"> + <h1 th:text="|${httpStatus} - ${httpStatus.reasonPhrase}|">404</h1> + <p th:utext="${errorMessage}">Error java.lang.NullPointerException</p> + <a href="/" th:href="@{/}" th:text="#{link.returnToMain}">Back to Home Page</a> +</body> +</html>
\ 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 9fd431925..bcfa318d3 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 @@ </div> </div> <div id="signin"> + <p th:if="${loginError}" class="error" th:text="#{error.login}">Произошла ошибка, проверьте имя пользователя и пароль</p> <a href="#" onclick="$('#signinform').toggle(); $('#nickinput').focus(); return false" th:text="#{question.areRegistered}">Уже зарегистрированы?</a> <div id="signinform"> - <p th:if="${loginError}" class="error" th:text="#{error.login}">Произошла ошибка, проверьте имя пользователя и пароль</p> <form action="#" th:action="@{/do_login}" method="POST"> <input class="txt" type="text" name="j_username" placeholder="Имя пользователя" th:placeholder="#{label.username}" id="nickinput"/> <input class="txt" type="password" name="j_password" placeholder="Пароль" th:placeholder="#{label.password}"/> 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 4162aa8b9..000000000 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/postNotFound.html +++ /dev/null @@ -1,23 +0,0 @@ -<!DOCTYPE html> -<html xmlns:th="http://www.thymeleaf.org" - xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" - layout:decorate="~{layout/mainLayout}"> -<head> - <title th:text="#{error.pageNotFound}">Страница не найдена</title> -</head> -<body> - -<section layout:fragment="content"> - <div id="pagetitle"> - <h1 th:text="#{error.pageNotFound}">Страница не найдена</h1> - </div> - - <div id="wrapper"> - <p th:text="#{errors.pageNotFound.extended(${messageId})}">Похоже, пользователь удалил страницу, возможна она и не была создана.</p> - </div> - <div> - <a th:href="@{/}" href="/"><span th:text="#{link.returnToMain}">Вернуться на главную</span></a> - </div> -</section> -</body> -</html>
\ 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 de1a97a0d..000000000 --- a/juick-spring-www/src/main/webapp/WEB-INF/templates/userNotFound.html +++ /dev/null @@ -1,22 +0,0 @@ -<!DOCTYPE html> -<html xmlns:th="http://www.thymeleaf.org" - xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" - layout:decorate="~{layout/mainLayout}"> -<head> - <title th:text="#{error.userNotFound}">Пользователь не найден</title> -</head> -<body> -<section layout:fragment="content"> - <div id="pagetitle"> - <h1 th:text="#{error.userNotFound}">Пользователь не найден</h1> - </div> - - <div id="wrapper"> - <p th:text="#{errors.userNotFound.extended(${userName})}">Пользователь не найден.</p> - </div> - <div> - <a th:href="@{/}" href="/"><span th:text="#{link.returnToMain}">Вернуться на главную</span></a> - </div> -</section> -</body> -</html>
\ No newline at end of file |