Skip to content

Commit

Permalink
11_09_404_error
Browse files Browse the repository at this point in the history
  • Loading branch information
GlyzinAI committed Sep 8, 2019
1 parent be8b443 commit 29f19f7
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 24 deletions.
1 change: 1 addition & 0 deletions config/messages/app.properties
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ error.appError=Application error
error.dataNotFound=Data not found
error.dataError=Data error
error.validationError=Validation error
error.wrongRequest=Wrong request

NotEmpty="{0}" must not be empty
NotBlank="{0}" must not be empty
Expand Down
1 change: 1 addition & 0 deletions config/messages/app_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ error.appError=Ошибка приложения
error.dataNotFound=Данные не найдены
error.dataError=Ошибка в данных
error.validationError=Ошибка проверки данных
error.wrongRequest=Неверный запрос

NotEmpty=Поле "{0}" не должно быть пустым
NotBlank=Поле "{0}" не должно быть пустым
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/ru/javawebinar/topjava/util/ValidationUtil.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package ru.javawebinar.topjava.util;

import org.slf4j.Logger;
import ru.javawebinar.topjava.HasId;
import ru.javawebinar.topjava.util.exception.ErrorType;
import ru.javawebinar.topjava.util.exception.IllegalRequestDataException;
import ru.javawebinar.topjava.util.exception.NotFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.*;
import java.util.Set;

Expand Down Expand Up @@ -77,4 +80,14 @@ public static <T> void validate(T bean) {
throw new ConstraintViolationException(violations);
}
}

public static Throwable logAndGetRootCause(Logger log, HttpServletRequest req, Exception e, boolean logException, ErrorType errorType) {
Throwable rootCause = ValidationUtil.getRootCause(e);
if (logException) {
log.error(errorType + " at request " + req.getRequestURL(), rootCause);
} else {
log.warn("{} at request {}: {}", errorType, req.getRequestURL(), rootCause.toString());
}
return rootCause;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ public enum ErrorType {
APP_ERROR("error.appError"),
DATA_NOT_FOUND("error.dataNotFound"),
DATA_ERROR("error.dataError"),
VALIDATION_ERROR("error.validationError");
VALIDATION_ERROR("error.validationError"),
WRONG_REQUEST("error.wrongRequest");

private final String errorCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,7 @@ public ErrorInfo handleError(HttpServletRequest req, Exception e) {
}

private ErrorInfo logAndGetErrorInfo(HttpServletRequest req, Exception e, boolean logException, ErrorType errorType, String... details) {
Throwable rootCause = ValidationUtil.getRootCause(e);
if (logException) {
log.error(errorType + " at request " + req.getRequestURL(), rootCause);
} else {
log.warn("{} at request {}: {}", errorType, req.getRequestURL(), rootCause.toString());
}
Throwable rootCause = ValidationUtil.logAndGetRootCause(log, req, e, logException, errorType);
return new ErrorInfo(req.getRequestURL(), errorType,
messageUtil.getMessage(errorType.getErrorCode()),
details.length != 0 ? details : new String[]{ValidationUtil.getMessage(rootCause)});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import ru.javawebinar.topjava.AuthorizedUser;
import ru.javawebinar.topjava.util.ValidationUtil;
import ru.javawebinar.topjava.util.exception.ErrorType;
Expand All @@ -19,12 +20,21 @@ public class GlobalControllerExceptionHandler {
@Autowired
private MessageUtil messageUtil;

@ExceptionHandler(NoHandlerFoundException.class)
public ModelAndView wrongRequest(HttpServletRequest req, NoHandlerFoundException e) throws Exception {
return logAndGetExceptionView(req, e, false, ErrorType.WRONG_REQUEST);
}

@ExceptionHandler(Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
log.error("Exception at request " + req.getRequestURL(), e);
Throwable rootCause = ValidationUtil.getRootCause(e);
return logAndGetExceptionView(req, e, true, ErrorType.APP_ERROR);
}

private ModelAndView logAndGetExceptionView(HttpServletRequest req, Exception e, boolean logException, ErrorType errorType) {
Throwable rootCause = ValidationUtil.logAndGetRootCause(log, req, e, logException, errorType);

ModelAndView mav = new ModelAndView("exception/exception");
mav.addObject("typeMessage", messageUtil.getMessage(ErrorType.APP_ERROR.getErrorCode()));
mav.addObject("typeMessage", messageUtil.getMessage(errorType.getErrorCode()));
mav.addObject("exception", rootCause);
mav.addObject("message", ValidationUtil.getMessage(rootCause));

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/spring/spring-mvc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<bean class="ru.javawebinar.topjava.web.json.JacksonObjectMapper" id="objectMapper" factory-method="getMapper"/>

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

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

Expand Down
4 changes: 4 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
Expand Down
13 changes: 0 additions & 13 deletions src/main/webapp/test.html

This file was deleted.

0 comments on commit 29f19f7

Please sign in to comment.