-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexception_handling.patch
184 lines (184 loc) · 6.77 KB
/
exception_handling.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
Index: src/main/java/com/handson/basic/util/RestErrorMessage.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/util/RestErrorMessage.java b/src/main/java/com/handson/basic/util/RestErrorMessage.java
new file mode 100644
--- /dev/null (date 1643232603469)
+++ b/src/main/java/com/handson/basic/util/RestErrorMessage.java (date 1643232603469)
@@ -0,0 +1,65 @@
+package com.handson.basic.util;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import java.util.Objects;
+
+@JsonAutoDetect
+@JsonInclude(JsonInclude.Include.NON_EMPTY)
+public class RestErrorMessage {
+ private String error;
+
+
+
+ @JsonProperty
+ public String getError() {
+ return error;
+ }
+
+
+ @Override
+ public String toString() {
+ return "RestErrorMessage{" +
+ "error='" + error + '\'' +
+ '}';
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ RestErrorMessage that = (RestErrorMessage) o;
+ return Objects.equals(error, that.error) ;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(error);
+ }
+
+
+ public static final class RestErrorMessageBuilder {
+ private String error;
+
+ private RestErrorMessageBuilder() {
+ }
+
+ public static RestErrorMessageBuilder aRestErrorMessage() {
+ return new RestErrorMessageBuilder();
+ }
+
+ public RestErrorMessageBuilder error(String error) {
+ this.error = error;
+ return this;
+ }
+
+
+ public RestErrorMessage build() {
+ RestErrorMessage restErrorMessage = new RestErrorMessage();
+ restErrorMessage.error = this.error;
+ return restErrorMessage;
+ }
+ }
+}
Index: src/main/java/com/handson/basic/util/GlobalDefaultExceptionHandler.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/util/GlobalDefaultExceptionHandler.java b/src/main/java/com/handson/basic/util/GlobalDefaultExceptionHandler.java
new file mode 100644
--- /dev/null (date 1643232603463)
+++ b/src/main/java/com/handson/basic/util/GlobalDefaultExceptionHandler.java (date 1643232603463)
@@ -0,0 +1,73 @@
+package com.handson.basic.util;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.dao.DuplicateKeyException;
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.MissingServletRequestParameterException;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+import javax.servlet.http.HttpServletRequest;
+import java.nio.file.AccessDeniedException;
+
+
+@ControllerAdvice
+class GlobalDefaultExceptionHandler {
+
+ private static final Logger logger = LoggerFactory.getLogger(GlobalDefaultExceptionHandler.class);
+
+ @Autowired
+ ObjectMapper om;
+
+ @ExceptionHandler(value = Exception.class)
+ public ResponseEntity<String> defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
+ logger.error("exception:" + e.getMessage() + ", class:" + e.getClass().getName());
+ // If the exception is annotated with @ResponseStatus rethrow it and let
+ // the framework handle it - like the OrderNotFoundException example
+ // at the start of this post.
+ // AnnotationUtils is a Spring Framework utility class.
+ if (AnnotationUtils.findAnnotation
+ (e.getClass(), ResponseStatus.class) != null) {
+ logger.error(e.getMessage(), e);
+ throw e;
+ }
+ if (e instanceof DuplicateKeyException) {
+ return new ResponseEntity<>(anErrorResponse("Duplicate Id"), HttpStatus.CONFLICT);
+ } else if (e instanceof EmptyResultDataAccessException) {
+ return new ResponseEntity<>(anErrorResponse("Not Found"), HttpStatus.NOT_FOUND);
+ // we raise runtime exceptions for business issues.
+ } else if (e instanceof MissingServletRequestParameterException) {
+ String res = anErrorResponse(e.getMessage());
+ return new ResponseEntity<>(res, HttpStatus.BAD_REQUEST);
+ } else if (e instanceof AccessDeniedException) {
+ String res = anErrorResponse(e.getMessage());
+ return new ResponseEntity<>(res, HttpStatus.FORBIDDEN);
+ } else if (e instanceof HandsonException) {
+ String res = anErrorResponse(e.getMessage());
+ return new ResponseEntity<>(res, HttpStatus.FORBIDDEN);
+ }
+ logger.error(e.getMessage(), e);
+ // Otherwise it is unexpected
+ return new ResponseEntity<>(anErrorResponse("unexpected problem"), HttpStatus.CONFLICT);
+ }
+
+ private String anErrorResponse(String s) {
+ RestErrorMessage em = RestErrorMessage.RestErrorMessageBuilder.aRestErrorMessage().error(s).build();
+ String res = null;
+ try {
+ res = om.writeValueAsString(em);
+ return res;
+ } catch (JsonProcessingException e) {
+ logger.error(e.getMessage(), e);
+ return "";
+ }
+ }
+}
Index: src/main/java/com/handson/basic/util/HandsonException.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/com/handson/basic/util/HandsonException.java b/src/main/java/com/handson/basic/util/HandsonException.java
new file mode 100644
--- /dev/null (date 1643232603466)
+++ b/src/main/java/com/handson/basic/util/HandsonException.java (date 1643232603466)
@@ -0,0 +1,16 @@
+package com.handson.basic.util;
+
+import java.util.function.Supplier;
+
+public class HandsonException extends RuntimeException implements Supplier<HandsonException> {
+
+ public HandsonException(String message) {
+ super(message);
+ }
+
+
+ @Override
+ public HandsonException get() {
+ return this;
+ }
+}