From 26dbf521e28d03c28497b03dc830b8d226e504fe Mon Sep 17 00:00:00 2001 From: mac_daehwan2yo Date: Sun, 10 Apr 2022 14:28:49 +0900 Subject: [PATCH] =?UTF-8?q?feat=20(#3)=20:=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../howtodo/error/ControllerExceptionHandler.java | 13 +++++++++++++ .../howtodo/web/comment/CommentController.java | 5 ++--- .../howtodo/web/exam/ExamController.java | 4 +++- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/codingwasabi/howtodo/error/ControllerExceptionHandler.java diff --git a/src/main/java/com/codingwasabi/howtodo/error/ControllerExceptionHandler.java b/src/main/java/com/codingwasabi/howtodo/error/ControllerExceptionHandler.java new file mode 100644 index 0000000..6b27057 --- /dev/null +++ b/src/main/java/com/codingwasabi/howtodo/error/ControllerExceptionHandler.java @@ -0,0 +1,13 @@ +package com.codingwasabi.howtodo.error; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class ControllerExceptionHandler { + @ExceptionHandler({IllegalArgumentException.class, IllegalStateException.class}) + public ResponseEntity handleIllegalException(RuntimeException e) { + return ResponseEntity.badRequest().body(e.getMessage()); + } +} diff --git a/src/main/java/com/codingwasabi/howtodo/web/comment/CommentController.java b/src/main/java/com/codingwasabi/howtodo/web/comment/CommentController.java index 1891525..5b22a05 100644 --- a/src/main/java/com/codingwasabi/howtodo/web/comment/CommentController.java +++ b/src/main/java/com/codingwasabi/howtodo/web/comment/CommentController.java @@ -38,14 +38,13 @@ public GetCommentsByUserIdResponse getCommentsByUserId(@PathVariable("accountId" } @PostMapping("/calendar/{accountId}/result/comments/{targetDate}") - public ResponseEntity createComments(@LoginAccount Account account, + public ResponseEntity createComments(@LoginAccount Account account, @PathVariable("accountId") Long accountId, @PathVariable("targetDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date, @RequestBody CreateCommentsRequest createCommentsRequest) { if (account.isAnonymous()) { - return ResponseEntity.badRequest() - .body("need authenticate before write comment"); + throw new IllegalStateException("need authenticate before write comment"); } Comment comment = Comment.builder() diff --git a/src/main/java/com/codingwasabi/howtodo/web/exam/ExamController.java b/src/main/java/com/codingwasabi/howtodo/web/exam/ExamController.java index 0a77707..d125b73 100644 --- a/src/main/java/com/codingwasabi/howtodo/web/exam/ExamController.java +++ b/src/main/java/com/codingwasabi/howtodo/web/exam/ExamController.java @@ -28,7 +28,9 @@ public class ExamController { @GetMapping("/my/exams") public GetMyExamResponse getMyExam(@LoginAccount Account account) { - + if(account.isAnonymous()) { + throw new IllegalStateException("need authenticate before retrieve my exam"); + } return new GetMyExamResponse(examService.getMyExam(account) .stream() .map(ExamResponse::new)