|
|
@@ -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
|
+
|