Skip to content

Commit 06ae855

Browse files
authored
Merge pull request #9 from kento-kotlin-sandbox/future/add_error_pages
Future/add error pages
2 parents f9d7b58 + 7243d16 commit 06ae855

File tree

6 files changed

+121
-2
lines changed

6 files changed

+121
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.demo.login.aspect;
2+
3+
import org.aspectj.lang.annotation.AfterThrowing;
4+
import org.aspectj.lang.annotation.Aspect;
5+
import org.springframework.dao.DataAccessException;
6+
import org.springframework.stereotype.Component;
7+
8+
9+
@Aspect
10+
@Component
11+
public class ErrorAspect {
12+
13+
@AfterThrowing(value="execution(* *..*..*(..))" + " && (bean(*Controller) || bean(*Service) || bean(*Repository))", throwing="ex")
14+
public void throwingNull(DataAccessException ex) {
15+
// 例外処理の内容(ログ出力)
16+
System.out.println("===========================================");
17+
System.out.println("DataAccessExceptionが発生しました。:" + ex);
18+
System.out.println("===========================================");
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.example.demo.login.controller;
2+
3+
import org.springframework.dao.DataAccessException;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.ui.Model;
7+
import org.springframework.web.bind.annotation.ControllerAdvice;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
10+
11+
@ControllerAdvice
12+
@Component
13+
public class GlobalControllerAdvice {
14+
15+
@ExceptionHandler(DataAccessException.class)
16+
public String dataAccessExceptionHandler(DataAccessException e, Model model) {
17+
// 例外クラスのメッセージをModelに登録
18+
model.addAttribute("error", "内部サーバーエラー(DB):GlobalControllAdvice");
19+
model.addAttribute("message", "DataAccessExceptionが発生しました。");
20+
// HTTPのエラーコード(500)をModelに登録
21+
model.addAttribute("status", HttpStatus.INTERNAL_SERVER_ERROR);
22+
23+
return "error";
24+
}
25+
26+
@ExceptionHandler(Exception.class)
27+
public String exceptionHandler(Exception e, Model model) {
28+
// 例外クラスのメッセージをModelに登録
29+
model.addAttribute("error", "内部サーバーエラー:GlobalControllAdvice");
30+
model.addAttribute("meaage", "Exceptionが発生しました。");
31+
32+
// HTTPエラーコード(500)をModelに登録
33+
model.addAttribute("status", HttpStatus.INTERNAL_SERVER_ERROR);
34+
35+
return "error";
36+
}
37+
}

src/main/java/com/example/demo/trySpring/login/controller/LoginController.java renamed to src/main/java/com/example/demo/login/controller/LoginController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.demo.trySpring.login.controller;
1+
package com.example.demo.login.controller;
22

33
import org.springframework.stereotype.Controller;
44
import org.springframework.ui.Model;

src/main/java/com/example/demo/trySpring/login/controller/SignupController.java renamed to src/main/java/com/example/demo/login/controller/SignupController.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
package com.example.demo.trySpring.login.controller;
1+
package com.example.demo.login.controller;
22

33
import java.util.LinkedHashMap;
44
import java.util.Map;
55

66
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.dao.DataAccessException;
8+
import org.springframework.http.HttpStatus;
79
import org.springframework.stereotype.Controller;
810
import org.springframework.ui.Model;
911
import org.springframework.validation.BindingResult;
1012
import org.springframework.validation.annotation.Validated;
13+
import org.springframework.web.bind.annotation.ExceptionHandler;
1114
import org.springframework.web.bind.annotation.GetMapping;
1215
import org.springframework.web.bind.annotation.ModelAttribute;
1316
import org.springframework.web.bind.annotation.PostMapping;
@@ -37,6 +40,36 @@ private Map<String, String> initRadioMarrige() {
3740
return radio;
3841
}
3942

43+
// ExceptionHandler(DB)
44+
@ExceptionHandler(DataAccessException.class)
45+
public String dataAccessExceptionHandler(DataAccessException e, Model model) {
46+
// 例外クラスのメッセージをModelに登録
47+
model.addAttribute("error", "内部サーバーエラー(DB):ExceptionHandler");
48+
49+
// 例外クラスのメッセージをModelに登録
50+
model.addAttribute("message", "SignupController で DataAccessExceptionが発生しました");
51+
52+
// HTTPのエラーコード(500)をModelに登録
53+
model.addAttribute("status", HttpStatus.INTERNAL_SERVER_ERROR);
54+
55+
return "error";
56+
}
57+
58+
// ExceptionHandler(基本)
59+
@ExceptionHandler(Exception.class)
60+
public String exceptionHandler(Exception e, Model model) {
61+
// 例外クラスのメッセージをModelに登録
62+
model.addAttribute("error", "内部サーバーエラー: ExceptionHandler");
63+
64+
// 例外クラスのメッセージをModelに登録
65+
model.addAttribute("message", "SignupController でExceptionが発生しました");
66+
67+
// HTTPのエラーコード(500)をModelに登録
68+
model.addAttribute("status", HttpStatus.INTERNAL_SERVER_ERROR);
69+
70+
return "error";
71+
}
72+
4073
// ユーザー登録画面のGET用コントローラー
4174
@GetMapping("/signup")
4275
public String getSignUp(@ModelAttribute SignupForm form, Model model) {
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Error</title>
6+
</head>
7+
<body>
8+
<h1 th:text="${status} + ' ' + ${error}"></h1>
9+
<p th:text="${message}"></p>
10+
<p>ログイン画面に戻ってください</p>
11+
<form method="post" th:action="@{/logout}">
12+
<button class="btn btn-link" type="submit">ログイン画面に戻る</button>
13+
</form>
14+
</body>
15+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>404Error</title>
6+
</head>
7+
<body>
8+
<h1 th:text="${status} + ' ' + ${error}"></h1>
9+
<p>リクエストされたページは存在しません。</p>
10+
<form method="post" th:action="@{/logout}">
11+
<button class="btn btn-link" type="submit">ログイン画面に戻る</button>
12+
</form>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)