|
@@ -1,30 +1,65 @@
|
1
|
1
|
package io.zipcoder.tc_spring_poll_application;
|
2
|
2
|
|
3
|
3
|
import io.zipcoder.tc_spring_poll_application.error.ErrorDetail;
|
|
4
|
+import io.zipcoder.tc_spring_poll_application.error.ValidationError;
|
4
|
5
|
import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
|
6
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
7
|
+import org.springframework.context.MessageSource;
|
5
|
8
|
import org.springframework.http.HttpStatus;
|
6
|
9
|
import org.springframework.http.ResponseEntity;
|
|
10
|
+import org.springframework.validation.FieldError;
|
|
11
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
7
|
12
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
8
|
13
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
9
|
14
|
|
10
|
15
|
import javax.servlet.http.HttpServletRequest;
|
|
16
|
+import java.util.ArrayList;
|
11
|
17
|
import java.util.Arrays;
|
12
|
18
|
import java.util.Date;
|
|
19
|
+import java.util.List;
|
13
|
20
|
|
14
|
21
|
@ControllerAdvice
|
15
|
22
|
public class RestExceptionHandler {
|
16
|
23
|
|
|
24
|
+ @Autowired
|
|
25
|
+ MessageSource messageSource;
|
|
26
|
+
|
17
|
27
|
@ExceptionHandler(ResourceNotFoundException.class)
|
18
|
28
|
public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
|
19
|
29
|
|
20
|
30
|
ErrorDetail ed = new ErrorDetail();
|
21
|
31
|
|
22
|
32
|
ed.setTimeStamp(new Date().getTime());
|
23
|
|
- ed.setTitle("Validation Failture");
|
|
33
|
+ ed.setTitle("Resource Not Found.");
|
24
|
34
|
ed.setStatus(404);
|
25
|
35
|
ed.setDetail("Unable to complete request or entity was not found.");
|
26
|
36
|
ed.setDeveloperMessage(Arrays.toString(rnfe.getStackTrace()));
|
27
|
37
|
|
28
|
38
|
return new ResponseEntity<>(ed, HttpStatus.NOT_FOUND);
|
29
|
39
|
}
|
|
40
|
+
|
|
41
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
42
|
+ public ResponseEntity<?>
|
|
43
|
+ handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request){
|
|
44
|
+ ErrorDetail ed = new ErrorDetail();
|
|
45
|
+
|
|
46
|
+ ed.setTimeStamp(new Date().getTime());
|
|
47
|
+ ed.setTitle("Validation Failure");
|
|
48
|
+ ed.setStatus(404);
|
|
49
|
+ ed.setDetail("Unable to complete request.");
|
|
50
|
+ ed.setDeveloperMessage(Arrays.toString(manve.getStackTrace()));
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+ List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
|
|
54
|
+ for(FieldError fe : fieldErrors) {
|
|
55
|
+
|
|
56
|
+ List<ValidationError> validationErrorList = ed.getErrors().computeIfAbsent(fe.getField(), k -> new ArrayList<>());
|
|
57
|
+ ValidationError validationError = new ValidationError();
|
|
58
|
+ validationError.setCode(fe.getCode());
|
|
59
|
+ validationError.setMessage(messageSource.getMessage(fe,null));
|
|
60
|
+ validationErrorList.add(validationError);
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ return new ResponseEntity<>(ed, HttpStatus.BAD_REQUEST);
|
|
64
|
+ }
|
30
|
65
|
}
|