|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+package io.zipcoder.tc_spring_poll_application.error;
|
|
|
2
|
+
|
|
|
3
|
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
|
|
|
4
|
+import org.springframework.http.HttpStatus;
|
|
|
5
|
+import org.springframework.http.ResponseEntity;
|
|
|
6
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
7
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
8
|
+
|
|
|
9
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
10
|
+import java.util.Arrays;
|
|
|
11
|
+import java.util.Date;
|
|
|
12
|
+
|
|
|
13
|
+/**
|
|
|
14
|
+ * project: spring-demo
|
|
|
15
|
+ * package: io.zipcoder.tc_spring_poll_application.error
|
|
|
16
|
+ * author: https://github.com/vvmk
|
|
|
17
|
+ * date: 4/7/18
|
|
|
18
|
+ */
|
|
|
19
|
+@ControllerAdvice
|
|
|
20
|
+public class RestExceptionHandler {
|
|
|
21
|
+ @ExceptionHandler(ResourceNotFoundException.class)
|
|
|
22
|
+ public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe,
|
|
|
23
|
+ HttpServletRequest request) {
|
|
|
24
|
+ ErrorDetail err = new ErrorDetail();
|
|
|
25
|
+ err.setTimestamp(new Date().getTime());
|
|
|
26
|
+ err.setStatus(HttpStatus.NOT_FOUND.value());
|
|
|
27
|
+ err.setDetail(rnfe.getMessage());
|
|
|
28
|
+ err.setDeveloperMessage(Arrays.toString(rnfe.getStackTrace()));
|
|
|
29
|
+ err.setTitle(rnfe.getClass().toString());
|
|
|
30
|
+ return new ResponseEntity<>(err, HttpStatus.NOT_FOUND);
|
|
|
31
|
+ }
|
|
|
32
|
+}
|