Przeglądaj źródła

Added Errors/Exception Handling

CWinarski 8 lat temu
rodzic
commit
cdb3651f04

+ 11
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Wyświetl plik

@@ -1,6 +1,7 @@
1 1
 package io.zipcoder.tc_spring_poll_application.controller;
2 2
 
3 3
 import io.zipcoder.tc_spring_poll_application.domain.Poll;
4
+import io.zipcoder.tc_spring_poll_application.exception.ResourceNotFoundException;
4 5
 import io.zipcoder.tc_spring_poll_application.repositories.PollRepository;
5 6
 import org.springframework.beans.factory.annotation.Autowired;
6 7
 import org.springframework.http.HttpHeaders;
@@ -43,6 +44,7 @@ public class PollController {
43 44
 
44 45
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.GET)
45 46
     public ResponseEntity<?> getPoll(@PathVariable Long pollId) {
47
+        verifyPoll(pollId);
46 48
         Poll p = pollRepository.findOne(pollId);
47 49
         return new ResponseEntity<> (p, HttpStatus.OK);
48 50
     }
@@ -50,14 +52,23 @@ public class PollController {
50 52
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
51 53
     public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
52 54
         // Save the entity
55
+        verifyPoll(pollId);
53 56
         Poll p = pollRepository.save(poll);
54 57
         return new ResponseEntity<>(HttpStatus.OK);
55 58
     }
56 59
 
57 60
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.DELETE)
58 61
     public ResponseEntity<?> deletePoll(@PathVariable Long pollId) {
62
+        verifyPoll(pollId);
59 63
         pollRepository.delete(pollId);
60 64
         return new ResponseEntity<>(HttpStatus.OK);
61 65
     }
62 66
 
67
+    public void verifyPoll(Long pollId){
68
+        Poll poll = pollRepository.findOne(pollId);
69
+        if(pollId == null){
70
+            throw new ResourceNotFoundException("The given poll id" + pollId + "does not exist!");
71
+        }
72
+    }
73
+
63 74
 }

+ 50
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/error/ErrorDetail.java Wyświetl plik

@@ -0,0 +1,50 @@
1
+package io.zipcoder.tc_spring_poll_application.error;
2
+
3
+public class ErrorDetail {
4
+
5
+    private String title; // title of error condition
6
+    private int status; // HTTP status code for current request
7
+    private String detail; // short readable description of error
8
+    private long timeStamp; // time in milliseconds when error occured
9
+    private String developerMessage; // detailed info as such exception class or trace
10
+
11
+    public String getTitle() {
12
+        return title;
13
+    }
14
+
15
+    public void setTitle(String title) {
16
+        this.title = title;
17
+    }
18
+
19
+    public int getStatus() {
20
+        return status;
21
+    }
22
+
23
+    public void setStatus(int status) {
24
+        this.status = status;
25
+    }
26
+
27
+    public String getDetail() {
28
+        return detail;
29
+    }
30
+
31
+    public void setDetail(String detail) {
32
+        this.detail = detail;
33
+    }
34
+
35
+    public long getTimeStamp() {
36
+        return timeStamp;
37
+    }
38
+
39
+    public void setTimeStamp(long timeStamp) {
40
+        this.timeStamp = timeStamp;
41
+    }
42
+
43
+    public String getDeveloperMessage() {
44
+        return developerMessage;
45
+    }
46
+
47
+    public void setDeveloperMessage(String developerMessage) {
48
+        this.developerMessage = developerMessage;
49
+    }
50
+}

+ 26
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Wyświetl plik

@@ -0,0 +1,26 @@
1
+package io.zipcoder.tc_spring_poll_application.exception;
2
+
3
+import io.zipcoder.tc_spring_poll_application.error.ErrorDetail;
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.Date;
11
+
12
+@ControllerAdvice
13
+public class RestExceptionHandler {
14
+
15
+    @ExceptionHandler(ResourceNotFoundException.class)
16
+    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request){
17
+        //make error detail object and fill it?
18
+        ErrorDetail errorDetail = new ErrorDetail();
19
+        errorDetail.setTitle("Resource Not Found");
20
+        errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
21
+        errorDetail.setDetail(rnfe.getMessage());
22
+        errorDetail.setTimeStamp(new Date().getTime());
23
+        errorDetail.setDeveloperMessage(rnfe.getClass().getName());
24
+        return new ResponseEntity<>(errorDetail, null,  HttpStatus.NOT_FOUND);
25
+    }
26
+}