From 9500eac50f76f95ca58c1156b7f067ade7adc34c Mon Sep 17 00:00:00 2001 From: s-choi Date: Thu, 10 Sep 2026 15:56:20 +0900 Subject: [PATCH] =?UTF-8?q?[EDIT]=20=E6=A8=A9=E9=99=90=E3=82=A8=E3=83=A9?= =?UTF-8?q?=E3=83=BC=E6=99=82=E3=81=AE=E9=81=B7=E7=A7=BB=E5=85=88=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aop/Handler/GlobalExceptionHandler.java | 122 +++++++----------- 1 file changed, 44 insertions(+), 78 deletions(-) diff --git a/src/main/java/com/example/rhizome/aop/Handler/GlobalExceptionHandler.java b/src/main/java/com/example/rhizome/aop/Handler/GlobalExceptionHandler.java index 8e5a8c3..2e7e1bb 100644 --- a/src/main/java/com/example/rhizome/aop/Handler/GlobalExceptionHandler.java +++ b/src/main/java/com/example/rhizome/aop/Handler/GlobalExceptionHandler.java @@ -1,111 +1,77 @@ package com.example.rhizome.aop.Handler; -import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword; -import com.example.rhizome.app.presentation.form.LoginInfoForm; -import com.example.rhizome.app.presentation.form.MessageForm; -import com.example.rhizome.exception.InsufficientPermissionException; -import com.example.rhizome.exception.NoSessionException; +import static com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAttributeKeyword.*; + +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 java.util.logging.Level; -import java.util.logging.Logger; +import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword; +import com.example.rhizome.app.presentation.form.LoginInfoForm; +import com.example.rhizome.app.presentation.form.MessageForm; +import com.example.rhizome.exception.InsufficientPermissionException; +import com.example.rhizome.exception.NoSessionException; -import static com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAttributeKeyword.LOGIN_INFO_FORM; -import static com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAttributeKeyword.MESSAGE_ERROR; - -/** - * 例外処理用のコントローラ - */ @ControllerAdvice public class GlobalExceptionHandler { - private final static Logger LOGGER = Logger.getLogger(GlobalExceptionHandler.class.getName()); + 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 = "不正なリクエストです。"; + private final String SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION = "不正な遷移です。"; + private final String SEVERE_ERROR_MESSAGE_BAD_REQUEST = "不正なリクエストです。"; - /** - * セッションが切れている場合の例外処理 - * - * @param model ビューにデータを渡すためのコンテナ - * @return 遷移先の画面 - */ - @ExceptionHandler(NoSessionException.class) - public String handleNoSessionException(Model model, Exception e) { + @ExceptionHandler(NoSessionException.class) + public String handleNoSessionException(Model model, Exception e) { - model.addAttribute(MESSAGE_ERROR, new MessageForm(SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION)); - model.addAttribute(LOGIN_INFO_FORM, new LoginInfoForm()); + model.addAttribute(MESSAGE_ERROR, new MessageForm(SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION)); + model.addAttribute(LOGIN_INFO_FORM, new LoginInfoForm()); - e.printStackTrace(); + e.printStackTrace(); - // エラーログ出力 - LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e); + LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e); - return TransitionTargetPageNameKeyword.LOGIN_HTML; - } + return TransitionTargetPageNameKeyword.LOGIN_HTML; + } - /** - * 権限がない場合の例外処理 - * - * @param model ビューにデータを渡すためのコンテナ - * @return 遷移先の画面 - */ - @ExceptionHandler(InsufficientPermissionException.class) - public String handleInsufficientPermissionException(Model model, Exception e) { + @ExceptionHandler(InsufficientPermissionException.class) + public String handleInsufficientPermissionException(Model model, Exception e) { - model.addAttribute(MESSAGE_ERROR, new MessageForm(SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION)); - model.addAttribute(LOGIN_INFO_FORM, new LoginInfoForm()); + model.addAttribute(MESSAGE_ERROR, new MessageForm(SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION)); + model.addAttribute(LOGIN_INFO_FORM, new LoginInfoForm()); - e.printStackTrace(); + e.printStackTrace(); - // エラーログ出力 - LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e); + LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e); - return TransitionTargetPageNameKeyword.LOGIN_HTML; + return TransitionTargetPageNameKeyword.ERROR_HTML; - } + } - /** - * リクエストメソッドが不正な場合の例外処理 - * - * @param model ビューにデータを渡すためのコンテナ - * @return 遷移先の画面 - */ - @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()); + @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(); + e.printStackTrace(); - // エラーログ出力 - LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e); + LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e); - // カスタムエラーページにリダイレクト - return TransitionTargetPageNameKeyword.ERROR_HTML; - } + return TransitionTargetPageNameKeyword.ERROR_HTML; + } - /** - * その他の例外処理 - * - * @param model ビューにデータを渡すためのコンテナ - * @return 遷移先の画面 - */ - @ExceptionHandler(Exception.class) - public String AllException(Model model, Exception e) { - // エラーメッセージやその他の情報をモデルに追加する - model.addAttribute("error", SEVERE_ERROR_MESSAGE_BAD_REQUEST); + @ExceptionHandler(Exception.class) + public String AllException(Model model, Exception e) { + model.addAttribute("error", SEVERE_ERROR_MESSAGE_BAD_REQUEST); - e.printStackTrace(); + e.printStackTrace(); - LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e); + LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE_ILLEGAL_TRANSITION, e); - // カスタムエラーページにリダイレクト - return TransitionTargetPageNameKeyword.ERROR_HTML; - } + return TransitionTargetPageNameKeyword.ERROR_HTML; + } }