|
|
@@ -1,17 +1,27 @@
|
|
1
|
1
|
package io.zipcoder.tc_spring_poll_application.exception;
|
|
2
|
2
|
|
|
3
|
3
|
import io.zipcoder.tc_spring_poll_application.error.ErrorDetail;
|
|
|
4
|
+import io.zipcoder.tc_spring_poll_application.error.ValidationError;
|
|
|
5
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
6
|
+import org.springframework.context.MessageSource;
|
|
4
|
7
|
import org.springframework.http.HttpStatus;
|
|
5
|
8
|
import org.springframework.http.ResponseEntity;
|
|
|
9
|
+import org.springframework.validation.FieldError;
|
|
|
10
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
6
|
11
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
7
|
12
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
8
|
13
|
|
|
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 {
|
|
14
|
21
|
|
|
|
22
|
+ @Autowired
|
|
|
23
|
+ MessageSource messageSource;
|
|
|
24
|
+
|
|
15
|
25
|
@ExceptionHandler(ResourceNotFoundException.class)
|
|
16
|
26
|
public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request){
|
|
17
|
27
|
//make error detail object and fill it?
|
|
|
@@ -23,4 +33,34 @@ public class RestExceptionHandler {
|
|
23
|
33
|
errorDetail.setDeveloperMessage(rnfe.getClass().getName());
|
|
24
|
34
|
return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
|
|
25
|
35
|
}
|
|
|
36
|
+
|
|
|
37
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
38
|
+ public ResponseEntity<?> handleValicationError(MethodArgumentNotValidException manve, HttpServletRequest request){
|
|
|
39
|
+ ErrorDetail errorDetail = new ErrorDetail();
|
|
|
40
|
+ errorDetail.setTitle("Validation Failed");
|
|
|
41
|
+ errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
|
42
|
+ errorDetail.setDetail("Input validation failed");
|
|
|
43
|
+ errorDetail.setTimeStamp(new Date().getTime());
|
|
|
44
|
+ errorDetail.setDeveloperMessage(manve.getClass().getName());
|
|
|
45
|
+
|
|
|
46
|
+ String requestPath = (String) request.getAttribute("javax.servlet.error. request_uri");
|
|
|
47
|
+ if(requestPath == null) {
|
|
|
48
|
+ requestPath = request.getRequestURI();
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
|
51
|
+ List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
|
|
|
52
|
+ for(FieldError fe : fieldErrors) {
|
|
|
53
|
+
|
|
|
54
|
+ List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
|
|
|
55
|
+ if(validationErrorList == null) {
|
|
|
56
|
+ validationErrorList = new ArrayList<>();
|
|
|
57
|
+ errorDetail.getErrors().put(fe.getField(), validationErrorList);
|
|
|
58
|
+ }
|
|
|
59
|
+ ValidationError validationError = new ValidationError();
|
|
|
60
|
+ validationError.setCode(fe.getCode());
|
|
|
61
|
+ validationError.setMessage(messageSource.getMessage(fe, null));
|
|
|
62
|
+ validationErrorList.add(validationError);
|
|
|
63
|
+ }
|
|
|
64
|
+ return new ResponseEntity<>(errorDetail, HttpStatus.BAD_REQUEST);
|
|
|
65
|
+ }
|
|
26
|
66
|
}
|