[DELETE/ADD] 例外Handlerを再作成しました。
This commit is contained in:
@@ -0,0 +1,61 @@
|
|||||||
|
package com.example.demo.aop.handler;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
|
||||||
|
import com.example.demo.exception.NoSessionException;
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
private final static Logger LOGGER = Logger.getLogger(GlobalExceptionHandler.class.getName());
|
||||||
|
|
||||||
|
private final String SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION = "不正な遷移です。";
|
||||||
|
private final String SEVERE_ERROR_MESSAGE_BAD_REQUEST = "不正なリクエストです。";
|
||||||
|
|
||||||
|
@ExceptionHandler(NoSessionException.class)
|
||||||
|
public String handleNoSessionException(Model model, Exception e) {
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e);
|
||||||
|
|
||||||
|
model.addAttribute(
|
||||||
|
"errorMessage",
|
||||||
|
"システムエラーが発生しました。"
|
||||||
|
+ "初めからやり直してください。");
|
||||||
|
|
||||||
|
return "error";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||||
|
public String handleBadRequestException(Model model, Exception e) {
|
||||||
|
model.addAttribute("error", SEVERE_ERROR_MESSAGE_BAD_REQUEST);
|
||||||
|
model.addAttribute("status", HttpStatus.BAD_REQUEST.value());
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e);
|
||||||
|
|
||||||
|
return "error";
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public String AllException(Model model, Exception e) {
|
||||||
|
model.addAttribute("error", SEVERE_ERROR_MESSAGE_BAD_REQUEST);
|
||||||
|
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e);
|
||||||
|
|
||||||
|
return "error";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package com.example.demo.exception;
|
|
||||||
|
|
||||||
import org.springframework.ui.Model;
|
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
||||||
|
|
||||||
@ControllerAdvice
|
|
||||||
public class GlobalExceptionHandler {
|
|
||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
|
||||||
public String handleException(
|
|
||||||
Exception e,
|
|
||||||
Model model) {
|
|
||||||
|
|
||||||
model.addAttribute(
|
|
||||||
"errorMessage",
|
|
||||||
"システムエラーが発生しました。"
|
|
||||||
+ "初めからやり直してください。");
|
|
||||||
|
|
||||||
return "error";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user