Ver código fonte

Rest exception handler

Mitch Taylor 8 anos atrás
pai
commit
98c930c3e9

+ 23
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Ver arquivo

@@ -0,0 +1,23 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
3
+import java.util.Date;
4
+import javax.servlet.http.HttpServletRequest;
5
+import dtos.error.ErrorDetail;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.web.bind.annotation.ControllerAdvice;
9
+import org.springframework.web.bind.annotation.ExceptionHandler;
10
+
11
+@ControllerAdvice
12
+public class RestExceptionHandler {
13
+    @ExceptionHandler(ResourceNotFoundException.class)
14
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
15
+        ErrorDetail errorDetail = new ErrorDetail();
16
+        errorDetail.setTimeStamp(new Date().getTime());
17
+        errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
18
+        errorDetail.setTitle("Resource Not Found");
19
+        errorDetail.setDetail(rnfe.getMessage());
20
+        errorDetail.setDeveloperMessage(rnfe.getClass().getName());
21
+        return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
22
+    }
23
+}