|
|
@@ -1,16 +1,33 @@
|
|
1
|
1
|
package quickpoll.io.zipcoder.tc_spring_poll_application.exception;
|
|
2
|
2
|
|
|
|
3
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
4
|
+import org.springframework.context.MessageSource;
|
|
3
|
5
|
import org.springframework.http.HttpStatus;
|
|
4
|
6
|
import org.springframework.http.ResponseEntity;
|
|
|
7
|
+import org.springframework.validation.FieldError;
|
|
|
8
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
5
|
9
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
10
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
11
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
12
|
+import org.springframework.web.bind.annotation.ResponseStatus;
|
|
6
|
13
|
import quickpoll.dtos.error.ErrorDetail;
|
|
|
14
|
+import quickpoll.dtos.error.ValidationError;
|
|
7
|
15
|
|
|
|
16
|
+
|
|
|
17
|
+import javax.inject.Inject;
|
|
8
|
18
|
import javax.servlet.http.HttpServletRequest;
|
|
|
19
|
+import java.util.ArrayList;
|
|
9
|
20
|
import java.util.Date;
|
|
|
21
|
+import java.util.List;
|
|
10
|
22
|
|
|
11
|
23
|
@ControllerAdvice
|
|
12
|
24
|
public class RestExceptionHandler {
|
|
13
|
25
|
|
|
|
26
|
+ @Autowired
|
|
|
27
|
+ private MessageSource messageSource;
|
|
|
28
|
+
|
|
|
29
|
+ @ExceptionHandler(ResourceNotFoundException.class)
|
|
|
30
|
+ @ResponseStatus(HttpStatus.NOT_FOUND)
|
|
14
|
31
|
public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
|
|
15
|
32
|
ErrorDetail errorDetail = new ErrorDetail();
|
|
16
|
33
|
errorDetail.setTitle("Resource Not Found");
|
|
|
@@ -21,4 +38,39 @@ public class RestExceptionHandler {
|
|
21
|
38
|
|
|
22
|
39
|
return new ResponseEntity<>(errorDetail, HttpStatus.NOT_FOUND);
|
|
23
|
40
|
}
|
|
|
41
|
+
|
|
|
42
|
+ @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
43
|
+ public @ResponseBody
|
|
|
44
|
+ ErrorDetail handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request) {
|
|
|
45
|
+ ErrorDetail errorDetail = new ErrorDetail();
|
|
|
46
|
+
|
|
|
47
|
+ errorDetail.setTimeStamp(new Date().getTime());
|
|
|
48
|
+ errorDetail.setStatus(HttpStatus.BAD_REQUEST.value());
|
|
|
49
|
+ String requestPath = (String) request.getAttribute("javax.servlet.error.request_uri");
|
|
|
50
|
+ if(requestPath == null) {
|
|
|
51
|
+ requestPath = request.getRequestURI();
|
|
|
52
|
+ }
|
|
|
53
|
+ errorDetail.setTitle("Validation Failed");
|
|
|
54
|
+ errorDetail.setDetail("Input validation failed");
|
|
|
55
|
+ errorDetail.setDeveloperMessage(manve.getClass().getName());
|
|
|
56
|
+
|
|
|
57
|
+ List<FieldError> fieldErrors = manve.getBindingResult().getFieldErrors();
|
|
|
58
|
+ //System.out.println(fieldErrors);
|
|
|
59
|
+ for(FieldError fe : fieldErrors) {
|
|
|
60
|
+ List<ValidationError> validationErrorList = errorDetail.getErrors().
|
|
|
61
|
+ get(fe.getField());
|
|
|
62
|
+ if(validationErrorList == null) {
|
|
|
63
|
+ }
|
|
|
64
|
+ validationErrorList = new ArrayList<ValidationError>();
|
|
|
65
|
+ errorDetail.getErrors().put(fe.getField(),
|
|
|
66
|
+ validationErrorList);
|
|
|
67
|
+ ValidationError validationError = new ValidationError();
|
|
|
68
|
+ validationError.setCode(fe.getCode());
|
|
|
69
|
+ validationError.setMessage(messageSource.getMessage(fe, null));
|
|
|
70
|
+ validationErrorList.add(validationError);
|
|
|
71
|
+ }
|
|
|
72
|
+ return errorDetail;
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+
|
|
24
|
76
|
}
|