Skip to content

Commit 35f9ecb

Browse files
committed
11_09_404_status
1 parent 1bf4ad8 commit 35f9ecb

7 files changed

Lines changed: 22 additions & 18 deletions

File tree

‎config/messages/app.properties‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ error.appError=Application error
5656
error.dataNotFound=Data not found
5757
error.dataError=Data error
5858
error.validationError=Validation error
59+
error.wrongRequest=Wrong request
5960
6061
NotEmpty=[{0}] must not be empty
6162
NotBlank=[{0}] must not be empty

‎config/messages/app_ru.properties‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ error.appError=Ошибка приложения
5656
error.dataNotFound=Данные не найдены
5757
error.dataError=Ошибка в данных
5858
error.validationError=Ошибка проверки данных
59+
error.wrongRequest=Неверный запрос
5960

6061
NotEmpty=[{0}] не должно быть пустым
6162
NotBlank=[{0}] не должно быть пустым

‎src/main/java/ru/javawebinar/topjava/util/exception/ErrorType.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ public enum ErrorType {
44
APP_ERROR("error.appError"),
55
DATA_NOT_FOUND("error.dataNotFound"),
66
DATA_ERROR("error.dataError"),
7-
VALIDATION_ERROR("error.validationError");
7+
VALIDATION_ERROR("error.validationError"),
8+
WRONG_REQUEST("error.wrongRequest");
89

910
private final String errorCode;
1011

‎src/main/java/ru/javawebinar/topjava/web/GlobalExceptionHandler.java‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.web.bind.annotation.ControllerAdvice;
88
import org.springframework.web.bind.annotation.ExceptionHandler;
99
import org.springframework.web.servlet.ModelAndView;
10+
import org.springframework.web.servlet.NoHandlerFoundException;
1011
import ru.javawebinar.topjava.AuthorizedUser;
1112
import ru.javawebinar.topjava.util.ValidationUtil;
1213
import ru.javawebinar.topjava.util.exception.ErrorType;
@@ -24,15 +25,23 @@ public GlobalExceptionHandler(MessageSourceAccessor messageSourceAccessor) {
2425
this.messageSourceAccessor = messageSourceAccessor;
2526
}
2627

28+
@ExceptionHandler(NoHandlerFoundException.class)
29+
public ModelAndView wrongRequest(HttpServletRequest req, NoHandlerFoundException e) throws Exception {
30+
return logAndGetExceptionView(req, e, false, ErrorType.WRONG_REQUEST, HttpStatus.BAD_REQUEST);
31+
}
32+
2733
@ExceptionHandler(Exception.class)
2834
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
2935
log.error("Exception at request " + req.getRequestURL(), e);
30-
Throwable rootCause = ValidationUtil.logAndGetRootCause(log, req, e, true, ErrorType.APP_ERROR);
36+
return logAndGetExceptionView(req, e, true, ErrorType.APP_ERROR, HttpStatus.INTERNAL_SERVER_ERROR);
37+
}
38+
39+
private ModelAndView logAndGetExceptionView(HttpServletRequest req, Exception e, boolean logException, ErrorType errorType, HttpStatus httpStatus) {
40+
Throwable rootCause = ValidationUtil.logAndGetRootCause(log, req, e, logException, errorType);
3141

32-
HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
3342
ModelAndView mav = new ModelAndView("exception",
3443
Map.of("exception", rootCause, "message", ValidationUtil.getMessage(rootCause),
35-
"typeMessage", messageSourceAccessor.getMessage(ErrorType.APP_ERROR.getErrorCode()),
44+
"typeMessage", messageSourceAccessor.getMessage(errorType.getErrorCode()),
3645
"status", httpStatus));
3746
mav.setStatus(httpStatus);
3847

‎src/main/resources/spring/spring-mvc.xml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
<bean class="ru.javawebinar.topjava.web.json.JacksonObjectMapper" id="objectMapper" factory-method="getMapper"/>
1212

1313
<!-- serve static resources (*.html, ...) from src/main/webapp/ -->
14-
<mvc:default-servlet-handler/>
14+
<!--https://stackoverflow.com/a/44393203/548473-->
15+
<!--<mvc:default-servlet-handler/>-->
1516

1617
<security:global-method-security secured-annotations="enabled" pre-post-annotations="enabled"/>
1718

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
<param-name>contextConfigLocation</param-name>
3131
<param-value>classpath:spring/spring-mvc.xml</param-value>
3232
</init-param>
33+
<init-param>
34+
<param-name>throwExceptionIfNoHandlerFound</param-name>
35+
<param-value>true</param-value>
36+
</init-param>
3337
<load-on-startup>1</load-on-startup>
3438
</servlet>
3539
<servlet-mapping>

‎src/main/webapp/test.html‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)