|
|
@@ -1,6 +1,7 @@
|
|
1
|
|
-package dtos.error;
|
|
|
1
|
+package io.zipcoder.tc_spring_poll_application.exception;
|
|
2
|
2
|
|
|
3
|
|
-import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
|
|
3
|
+import dtos.error.ErrorDetail;
|
|
|
4
|
+import dtos.error.ValidationError;
|
|
4
|
5
|
import org.springframework.http.HttpStatus;
|
|
5
|
6
|
import org.springframework.http.ResponseEntity;
|
|
6
|
7
|
import org.springframework.validation.FieldError;
|
|
|
@@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
9
|
10
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
10
|
11
|
|
|
11
|
12
|
import javax.servlet.http.HttpServletRequest;
|
|
|
13
|
+import java.util.ArrayList;
|
|
12
|
14
|
import java.util.Date;
|
|
13
|
15
|
import java.util.List;
|
|
14
|
16
|
|
|
|
@@ -16,14 +18,15 @@ import java.util.List;
|
|
16
|
18
|
public class RestExceptionHandler {
|
|
17
|
19
|
|
|
18
|
20
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
19
|
|
- public ResponseEntity<?> handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request){
|
|
|
21
|
+ public ResponseEntity<?>
|
|
|
22
|
+ handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request) {
|
|
20
|
23
|
|
|
21
|
24
|
ErrorDetail errorDetail = new ErrorDetail();
|
|
22
|
25
|
//Populate
|
|
23
|
26
|
errorDetail.setTimeStamp(new Date().getTime());
|
|
24
|
27
|
errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
25
|
28
|
String requestPath = (String) request.getAttribute("javax.servlet.error.request_uri");
|
|
26
|
|
- if(requestPath == null){
|
|
|
29
|
+ if (requestPath == null) {
|
|
27
|
30
|
requestPath = request.getRequestURI();
|
|
28
|
31
|
}
|
|
29
|
32
|
errorDetail.setTitle("Validation Failed");
|
|
|
@@ -32,12 +35,23 @@ public class RestExceptionHandler {
|
|
32
|
35
|
|
|
33
|
36
|
//Create Validation Error instances
|
|
34
|
37
|
List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
|
|
35
|
|
- for(FieldError fe: fieldErrors){
|
|
|
38
|
+ for (FieldError fe : fieldErrors) {
|
|
36
|
39
|
List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
|
|
|
40
|
+ if (validationErrorList == null) {
|
|
|
41
|
+ validationErrorList = new ArrayList<ValidationError>();
|
|
|
42
|
+ pwerrorDetail.getErrors().put(fe.getField(), validationErrorList);
|
|
|
43
|
+ }
|
|
|
44
|
+ ValidationError validationError = new ValidationError();
|
|
|
45
|
+ validationError.setCode(fe.getCode());
|
|
|
46
|
+ validationError.setMessage(fe.getDefaultMessage());
|
|
|
47
|
+ validationErrorList.add(validationError);
|
|
37
|
48
|
|
|
38
|
49
|
}
|
|
|
50
|
+
|
|
|
51
|
+ return new ResponseEntity<>(errorDetail, null, HttpStatus.BAD_REQUEST);
|
|
39
|
52
|
}
|
|
40
|
53
|
|
|
|
54
|
+}
|
|
41
|
55
|
|
|
42
|
56
|
// @ExceptionHandler(ResourceNotFoundException.class)
|
|
43
|
57
|
// public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
|
|
|
@@ -54,4 +68,4 @@ public class RestExceptionHandler {
|
|
54
|
68
|
|
|
55
|
69
|
|
|
56
|
70
|
|
|
57
|
|
-}
|
|
|
71
|
+
|