|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+package io.zipcoder.tc_spring_poll_application.exception;
|
|
|
2
|
+
|
|
|
3
|
+import io.zipcoder.tc_spring_poll_application.error.ErrorDetail;
|
|
|
4
|
+import io.zipcoder.tc_spring_poll_application.error.ValidationError;
|
|
|
5
|
+import org.springframework.http.HttpStatus;
|
|
|
6
|
+import org.springframework.http.ResponseEntity;
|
|
|
7
|
+import org.springframework.validation.FieldError;
|
|
|
8
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
9
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
10
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
11
|
+import sun.tools.tree.FieldExpression;
|
|
|
12
|
+
|
|
|
13
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
14
|
+import java.lang.reflect.Field;
|
|
|
15
|
+import java.util.ArrayList;
|
|
|
16
|
+import java.util.Date;
|
|
|
17
|
+import java.util.List;
|
|
|
18
|
+
|
|
|
19
|
+@ControllerAdvice
|
|
|
20
|
+public class RestExceptionHandler {
|
|
|
21
|
+
|
|
|
22
|
+ // @ExceptionHandler(ResourceNotFoundException.class)
|
|
|
23
|
+// public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
|
|
|
24
|
+// ErrorDetail errorDetail = new ErrorDetail();
|
|
|
25
|
+// errorDetail.setTimeStamp(new Date().getTime());
|
|
|
26
|
+// errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
|
|
|
27
|
+// errorDetail.setTitle("Resource Not Found");
|
|
|
28
|
+// errorDetail.setDetail(rnfe.getMessage());
|
|
|
29
|
+// errorDetail.setDeveloperMessage(rnfe.getClass().getName());
|
|
|
30
|
+// return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
|
|
|
31
|
+// }
|
|
|
32
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
33
|
+ public ResponseEntity<?>
|
|
|
34
|
+ handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request) {
|
|
|
35
|
+
|
|
|
36
|
+ ErrorDetail errorDetail = new ErrorDetail();
|
|
|
37
|
+ // Populate errorDetail instance
|
|
|
38
|
+ errorDetail.setTimeStamp(new Date().getTime());
|
|
|
39
|
+ errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
|
40
|
+ String requestPath = (String) request.getAttribute("javax.servlet.error.request_uri");
|
|
|
41
|
+ if (requestPath == null) {
|
|
|
42
|
+ requestPath = request.getRequestURI();
|
|
|
43
|
+ }
|
|
|
44
|
+ errorDetail.setTitle("Validation Failed");
|
|
|
45
|
+ errorDetail.setDetail("Input validation failed");
|
|
|
46
|
+ errorDetail.setDeveloperMessage(manve.getClass().getName());
|
|
|
47
|
+
|
|
|
48
|
+ List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
|
|
|
49
|
+ for (FieldError fe : fieldErrors) {
|
|
|
50
|
+ List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
|
|
|
51
|
+ if (validationErrorList == null) {
|
|
|
52
|
+ validationErrorList = new ArrayList<ValidationError>();
|
|
|
53
|
+ errorDetail.getErrors().put(fe.getField(), validationErrorList);
|
|
|
54
|
+ }
|
|
|
55
|
+ ValidationError validationError = new ValidationError();
|
|
|
56
|
+ validationError.setCode(fe.getCode());
|
|
|
57
|
+ validationError.setMessage(fe.getDefaultMessage());
|
|
|
58
|
+ validationErrorList.add(validationError);
|
|
|
59
|
+ }
|
|
|
60
|
+ return new ResponseEntity<>(errorDetail, null, HttpStatus.BAD_REQUEST);
|
|
|
61
|
+
|
|
|
62
|
+ }
|
|
|
63
|
+ /** handleResourceNotFoundException method removed **/
|
|
|
64
|
+}
|