Skip to content

Commit a6dcc90

Browse files
committed
/error/500: add simple error handling page.
@see http://www.tutorialspoint.com/servlets/servlets-exception-handling.htm Fix GH #3
1 parent fd058ca commit a6dcc90

File tree

7 files changed

+117
-1
lines changed

7 files changed

+117
-1
lines changed

src/main/config/checkstyle-suppressions.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
<suppress checks="LineLength" files="AbstractPageWithForm.java" lines="27,28,33" />
99
<suppress checks="LineLength" files="Form.java" lines="31,32,37" />
10-
<suppress checks="LineLength" files="Url.java" lines="72" />
10+
<suppress checks="LineLength" files="Url.java" lines="73" />
11+
<suppress checks="LineLength" files="ErrorController.java" lines="74" />
1112

1213
<!-- false positives due to Lombok usage -->
1314
<suppress checks="HideUtilityClassConstructor" files="ru.mystamps.web.model" />

src/main/java/ru/mystamps/web/Url.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public final class Url {
6464
// see also error-page definitions at src/main/webapp/WEB-INF/web.xml
6565
public static final String UNAUTHORIZED_PAGE = "/error/401";
6666
public static final String NOT_FOUND_PAGE = "/error/404";
67+
public static final String INTERNAL_ERROR_PAGE = "/error/500";
6768

6869
// resources
6970
public static final String FAVICON_ICO = "/favicon.ico";

src/main/java/ru/mystamps/web/controller/ErrorController.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,31 @@ public void notFound(
6666
}
6767
}
6868

69+
@RequestMapping(value = Url.INTERNAL_ERROR_PAGE, method = RequestMethod.GET)
70+
public void internalError(HttpServletRequest request) {
71+
// TODO: log to database (with *.status_code, *.message, *.servlet_name and user details)
72+
73+
String page = (String)request.getAttribute("javax.servlet.error.request_uri");
74+
Class<?> exceptionType = (Class<?>)request.getAttribute("javax.servlet.error.exception_type");
75+
Exception exception = (Exception)request.getAttribute("javax.servlet.error.exception");
76+
77+
if (page != null && !Url.INTERNAL_ERROR_PAGE.equals(page)) {
78+
String msg = String.format(
79+
"Exception '%s' occurred at page %s",
80+
getNameOrAsIs(exceptionType),
81+
page
82+
);
83+
LOG.error(msg, exception);
84+
}
85+
}
86+
87+
private static Object getNameOrAsIs(Class<?> clazz) {
88+
if (clazz == null) {
89+
return null;
90+
}
91+
92+
return clazz.getName();
93+
}
94+
6995
}
7096

src/main/resources/ru/mystamps/i18n/Messages.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ t_401_description = Authorization required{0}to access page
6060
t_404_title = 404: page not found
6161
t_404_description = Requested page{0}not found
6262

63+
# error/500.html
64+
t_500_title = 500: internal server error
65+
t_500_description = Internal{0}server error
66+
6367
# account/auth.html
6468
t_auth_title = authentication
6569
t_activation_successful = Account successfully activated! Now you can pass authentication.

src/main/resources/ru/mystamps/i18n/Messages_ru.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ t_401_description = Необходима авторизация{0}для дос
6060
t_404_title = 404: страница не найдена
6161
t_404_description = Запрашиваемая страница{0}не найдена
6262

63+
# error/500.html
64+
t_500_title = 500: внутренняя ошибка сервера
65+
t_500_description = Внутренняя{0}ошибка сервера
66+
6367
# account/auth.html
6468
t_auth_title = идентификация
6569
t_activation_successful = Аккаунт успешно активирован! Теперь вы можете пройти идентификацию.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org"
4+
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
5+
<head>
6+
<meta charset="utf-8" />
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
8+
<title th:text="#{t_500_title}">500: internal server error</title>
9+
<link rel="shortcut icon" type="image/x-icon" href="../../../favicon.ico" th:href="${FAVICON_ICO}" />
10+
<link rel="stylesheet" href="http://yandex.st/bootstrap/2.3.1/css/bootstrap.min.css" th:href="${BOOTSTRAP_CSS}" />
11+
<link rel="stylesheet" href="http://yandex.st/bootstrap/2.3.1/css/bootstrap-responsive.min.css" th:href="${BOOTSTRAP_RESPONSIVE_CSS}" />
12+
<link rel="stylesheet" href="../../static/styles/main.css" th:href="${MAIN_CSS}" />
13+
</head>
14+
<body>
15+
<div class="row-fluid">
16+
<div id="header" class="span12">
17+
18+
<div id="logo" class="span10">
19+
<a href="../site/index.html" th:href="'/'" th:text="#{t_my_stamps}">My stamps</a>
20+
</div>
21+
22+
<div id="user_bar" class="span2">
23+
<ul class="unstyled">
24+
<!--/*/
25+
<li sec:authorize="isAuthenticated()">
26+
<i class="icon-user"></i>
27+
<a sec:authentication="principal.user.name"
28+
href="../collection/info.html"
29+
title="Open my collection"
30+
th:title="#{t_open_my_collection}"
31+
th:href="@{${INFO_COLLECTION_PAGE}(id=${#authentication.principal.user.collection.id})}">
32+
John Doe
33+
</a>
34+
</li>
35+
/*/-->
36+
<li sec:authorize="isAnonymous()">
37+
<a href="../account/auth.html" th:href="@{${AUTHENTICATION_PAGE}}" th:text="#{t_enter}">Sign in</a>
38+
</li>
39+
<!--/*/
40+
<li sec:authorize="isAuthenticated()">
41+
<i class="icon-share"></i>
42+
<a href="../site/index.html" th:href="@{${LOGOUT_PAGE}}" th:text="#{t_logout}">Sign out</a>
43+
</li>
44+
/*/-->
45+
<li sec:authorize="isAnonymous()">
46+
<a href="../account/register.html" th:href="@{${REGISTRATION_PAGE}}" th:text="#{t_register}">Register</a>
47+
</li>
48+
</ul>
49+
</div>
50+
51+
</div>
52+
</div>
53+
54+
<div class="row-fluid text-center">
55+
<div id="content" class="span12">
56+
<h1 id="error-code">
57+
500
58+
</h1>
59+
<h4 id="error-msg" th:utext="#{t_500_description('&lt;br /&gt;')}">
60+
Internal<br />server error
61+
</h4>
62+
</div>
63+
</div>
64+
65+
<div class="row-fluid">
66+
<footer class="text-right">
67+
<i class="icon-envelope"></i>
68+
<a href="mailto:slava.semushin@gmail.com" title="Write e-mail" th:href="|mailto:#{t_site_author_email}|" th:title="#{t_write_email}" th:text="#{t_site_author_name}">Slava Semushin</a>, 2009-2014
69+
</footer>
70+
</div>
71+
72+
<!-- Placed at the end of the document so the pages load faster -->
73+
<script src="http://yandex.st/bootstrap/2.3.1/js/bootstrap.min.js" th:src="${BOOTSTRAP_JS}"></script>
74+
</body>
75+
</html>

src/main/webapp/WEB-INF/web.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@
7979
<location>/error/404</location>
8080
</error-page>
8181

82+
<error-page>
83+
<exception-type>java.lang.Exception</exception-type>
84+
<location>/error/500</location>
85+
</error-page>
86+
8287
<filter>
8388
<filter-name>CharsetFilter</filter-name>
8489
<filter-class>

0 commit comments

Comments
 (0)