Kaitrina High 8 лет назад
Родитель
Сommit
d865a9530c

+ 13
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ErrorDetail.java Просмотреть файл

@@ -1,5 +1,8 @@
1 1
 package io.zipcoder.tc_spring_poll_application.dto.error;
2 2
 
3
+import java.util.List;
4
+import java.util.Map;
5
+
3 6
 public class ErrorDetail {
4 7
 
5 8
     private String title;
@@ -7,6 +10,7 @@ public class ErrorDetail {
7 10
     private String detail;
8 11
     private long timeStamp;
9 12
     private String developerMessage;
13
+    private Map<String, List<ValidationError>> errors;
10 14
 
11 15
     /**
12 16
      *
@@ -68,5 +72,14 @@ public class ErrorDetail {
68 72
     public void setDeveloperMessage(String developerMessage) {
69 73
         this.developerMessage = developerMessage;
70 74
     }
75
+
76
+
77
+    public Map<String, List<ValidationError>> getErrors() {
78
+        return errors;
79
+    }
80
+
81
+    public void setErrors(Map<String, List<ValidationError>> errors) {
82
+        this.errors = errors;
83
+    }
71 84
 }
72 85
 

+ 29
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dto/error/ValidationError.java Просмотреть файл

@@ -0,0 +1,29 @@
1
+package io.zipcoder.tc_spring_poll_application.dto.error;
2
+
3
+import java.util.List;
4
+import java.util.Map;
5
+
6
+public class ValidationError {
7
+
8
+    private String code;
9
+    private String message;
10
+
11
+    public String getCode() {
12
+        return code;
13
+    }
14
+
15
+    public void setCode(String code) {
16
+        this.code = code;
17
+    }
18
+
19
+    public String getMessage() {
20
+        return message;
21
+    }
22
+
23
+    public void setMessage(String message) {
24
+        this.message = message;
25
+    }
26
+
27
+}
28
+
29
+

+ 46
- 1
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Просмотреть файл

@@ -1,18 +1,31 @@
1 1
 package io.zipcoder.tc_spring_poll_application.exception;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.dto.error.ErrorDetail;
4
+import io.zipcoder.tc_spring_poll_application.dto.error.ValidationError;
5
+import org.springframework.context.MessageSource;
4 6
 import org.springframework.http.HttpStatus;
5 7
 import org.springframework.http.ResponseEntity;
8
+import org.springframework.validation.FieldError;
9
+import org.springframework.web.bind.MethodArgumentNotValidException;
6 10
 import org.springframework.web.bind.annotation.ControllerAdvice;
7 11
 import org.springframework.web.bind.annotation.ExceptionHandler;
8 12
 
13
+import javax.inject.Inject;
9 14
 import javax.servlet.http.HttpServletRequest;
15
+import java.util.ArrayList;
10 16
 import java.util.Date;
17
+import java.util.List;
11 18
 
12 19
 @ControllerAdvice
13 20
 public class RestExceptionHandler {
21
+
22
+    @Inject
23
+     private MessageSource messageSource;
24
+
14 25
     @ExceptionHandler(ResourceNotFoundException.class)
15
-    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
26
+    public ResponseEntity<?> handleResourceNotFoundException(
27
+            ResourceNotFoundException rnfe,
28
+            HttpServletRequest request) {
16 29
         ErrorDetail customError = new ErrorDetail();
17 30
         customError.setDetail(rnfe.getMessage());
18 31
         customError.setDeveloperMessage(rnfe.getStackTrace().toString());
@@ -22,6 +35,29 @@ public class RestExceptionHandler {
22 35
 
23 36
         return new ResponseEntity<>(customError, null, HttpStatus.NOT_FOUND);
24 37
     }
38
+
39
+    @ExceptionHandler(MethodArgumentNotValidException.class)
40
+    public ResponseEntity<?> handleValidationError(
41
+            MethodArgumentNotValidException manve,
42
+            HttpServletRequest request){
43
+        ErrorDetail customError = new ErrorDetail();
44
+
45
+        List<FieldError> fieldErrors =  manve.getBindingResult().getFieldErrors();
46
+        for(FieldError fe : fieldErrors) {
47
+
48
+            List<ValidationError> validationErrorList = customError.getErrors().get(fe.getField());
49
+            if(validationErrorList == null) {
50
+                validationErrorList = new ArrayList<>();
51
+                customError.getErrors().put(fe.getField(), validationErrorList);
52
+            }
53
+            ValidationError validationError = new ValidationError();
54
+            validationError.setCode(fe.getCode());
55
+            validationError.setMessage(messageSource.getMessage(fe, null));
56
+            validationErrorList.add(validationError);
57
+        }
58
+
59
+        return new ResponseEntity<>(customError, null, HttpStatus.NOT_FOUND);
60
+        }
25 61
 }
26 62
 
27 63
 
@@ -31,3 +67,12 @@ public class RestExceptionHandler {
31 67
 //return a ResponseEntity containing the ErrorDetail and an HTTP NOT_FOUND status
32 68
 //Use java.util's new Date().getTime() for the timestamp
33 69
 //Provide the detail and developer messages from the ResourceNotFoundException
70
+
71
+
72
+//In this handler we need to do the following:
73
+//
74
+// Create the ErrorDetail object (similar to before)
75
+//  Get the list of field validation errors
76
+// For each field error, add it to the appropriate list in the ErrorDetail (see below)
77
+// Return a ResponseEntity containing the error detail and the appropriate HTTP status code (400 Bad Request)
78
+

+ 2
- 0
src/main/resources/messages.properties Просмотреть файл

@@ -0,0 +1,2 @@
1
+NotEmpty.poll.question=Question is a required field
2
+Size.poll.options=Options must be greater than {2} and less than {1}