1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.exception.spring.boot.autoconfigure;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import lombok.AllArgsConstructor;
22 import lombok.Data;
23 import lombok.EqualsAndHashCode;
24 import lombok.Getter;
25 import lombok.NoArgsConstructor;
26 import lombok.Setter;
27 import lombok.ToString;
28 import org.bremersee.exception.RestApiExceptionMapperProperties;
29 import org.bremersee.exception.RestApiExceptionMapperProperties.ExceptionMapping;
30 import org.bremersee.exception.RestApiExceptionMapperProperties.ExceptionMappingConfig;
31 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
32 import org.springframework.boot.context.properties.ConfigurationProperties;
33 import org.springframework.http.HttpStatus;
34
35
36
37
38
39
40 @ConditionalOnClass(RestApiExceptionMapperProperties.class)
41 @ConfigurationProperties(prefix = "bremersee.exception-mapping")
42 @Getter
43 @Setter
44 @ToString
45 @EqualsAndHashCode
46 public class RestApiExceptionMapperBootProperties {
47
48
49
50
51 private List<String> apiPaths = new ArrayList<>();
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 private ExceptionMappingImpl defaultExceptionMapping;
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 private List<ExceptionMappingImpl> exceptionMappings = new ArrayList<>();
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 private ExceptionMappingConfigImpl defaultExceptionMappingConfig;
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 private List<ExceptionMappingConfigImpl> exceptionMappingConfigs = new ArrayList<>();
170
171
172
173
174 public RestApiExceptionMapperBootProperties() {
175
176 defaultExceptionMapping = new ExceptionMappingImpl();
177 defaultExceptionMapping.setExceptionClassName("*");
178 defaultExceptionMapping.setMessage(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
179 defaultExceptionMapping.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
180
181 defaultExceptionMappingConfig = new ExceptionMappingConfigImpl();
182 defaultExceptionMappingConfig.setExceptionClassName("*");
183
184 exceptionMappings.add(new ExceptionMappingImpl(
185 IllegalArgumentException.class.getName(),
186 HttpStatus.BAD_REQUEST,
187 null));
188
189 exceptionMappings.add(new ExceptionMappingImpl(
190 "org.springframework.security.access.AccessDeniedException",
191 HttpStatus.FORBIDDEN,
192 null));
193
194 exceptionMappings.add(new ExceptionMappingImpl(
195 "javax.persistence.EntityNotFoundException",
196 HttpStatus.NOT_FOUND,
197 null));
198 }
199
200
201
202
203
204
205 public RestApiExceptionMapperProperties toRestApiExceptionMapperProperties() {
206 return RestApiExceptionMapperProperties.builder()
207 .defaultExceptionMapping(getDefaultExceptionMapping())
208 .exceptionMappings(getExceptionMappings())
209 .defaultExceptionMappingConfig(getDefaultExceptionMappingConfig())
210 .exceptionMappingConfigs(getExceptionMappingConfigs())
211 .build();
212 }
213
214
215
216
217 @Setter
218 @ToString
219 @EqualsAndHashCode
220 @NoArgsConstructor
221 @AllArgsConstructor
222 public static class ExceptionMappingImpl implements ExceptionMapping {
223
224
225
226
227
228
229
230
231 public ExceptionMappingImpl(String exceptionClassName, HttpStatus httpStatus, String code) {
232 this.exceptionClassName = exceptionClassName;
233 if (httpStatus != null) {
234 this.status = httpStatus.value();
235 this.message = httpStatus.getReasonPhrase();
236 }
237 this.code = code;
238 }
239
240
241
242
243 @Getter
244 private String exceptionClassName;
245
246 private int status;
247
248
249
250
251 @Getter
252 private String message;
253
254
255
256
257 @Getter
258 private String code;
259
260
261
262
263
264
265 @Override
266 public int getStatus() {
267 if (HttpStatus.resolve(status) == null) {
268 return HttpStatus.INTERNAL_SERVER_ERROR.value();
269 }
270 return status;
271 }
272
273 }
274
275
276
277
278 @Data
279 public static class ExceptionMappingConfigImpl implements ExceptionMappingConfig {
280
281 private String exceptionClassName;
282
283 private Boolean includeMessage = true;
284
285 private Boolean includeException = true;
286
287 private Boolean includeApplicationName = true;
288
289 private Boolean includePath = true;
290
291 private Boolean includeHandler = false;
292
293 private Boolean includeStackTrace = false;
294
295 private Boolean includeCause = false;
296
297 private Boolean evaluateAnnotationFirst = false;
298
299
300
301
302 public ExceptionMappingConfigImpl() {
303 super();
304 }
305
306 }
307
308 }