From 3fd27b31f87c28832c9ef1f19232f913c5e1d06e Mon Sep 17 00:00:00 2001 From: sominyun <128496121+sominyun@users.noreply.github.com> Date: Fri, 1 Mar 2024 14:21:35 +0900 Subject: [PATCH 1/3] =?UTF-8?q?chore:=20issue=20template=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/ISSUE_TEMPLATE/issue-template.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/issue-template.md diff --git a/.github/ISSUE_TEMPLATE/issue-template.md b/.github/ISSUE_TEMPLATE/issue-template.md new file mode 100644 index 0000000..28edae2 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/issue-template.md @@ -0,0 +1,17 @@ +--- +name: issue template +about: 이슈 템플릿 자동 생성 +title: '' +labels: '' +assignees: '' + +--- + +## 📝 Content + +- 내용 작성 + +## ✅ TODO + +- [ ] 작업1 +- [ ] 작업2 From 2e0d98a91f1a3e0ebdaab012401c2cd805467782 Mon Sep 17 00:00:00 2001 From: sominyun <128496121+sominyun@users.noreply.github.com> Date: Fri, 1 Mar 2024 14:23:22 +0900 Subject: [PATCH 2/3] =?UTF-8?q?chore:=20pr=20template=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/PULL_REQUEST_TEMPLATE.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index bcc5c1a..b781323 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,12 +1,11 @@ -### 관련 이슈 +## 🗃 Issue -- [이슈번호] -- 또는 close [이슈번호] +- Close #이슈번호 -### PR Checklist for Reviewer +## 🔥 Task -- [ ] -- [ ] -- [ ] +- 작업 내용 -### 테스트 결과 +## 📄 Reference + +- None From 70bb951f2840f77406f17286973f739840291127 Mon Sep 17 00:00:00 2001 From: Somin Yun Date: Fri, 1 Mar 2024 14:28:20 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20#4=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ExceptionController.java | 24 +++++++++ .../soullive_a/controller/MainController.java | 1 - .../exception/base/BaseException.java | 26 ++++++++++ .../exception/base/BaseResponse.java | 25 +++++++++ .../base/CustomExceptionHandler.java | 52 +++++++++++++++++++ .../soullive_a/exception/base/ErrorCode.java | 29 +++++++++++ .../custom/NotFoundUserException.java | 17 ++++++ 7 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/backend/soullive_a/controller/ExceptionController.java create mode 100644 src/main/java/com/backend/soullive_a/exception/base/BaseException.java create mode 100644 src/main/java/com/backend/soullive_a/exception/base/BaseResponse.java create mode 100644 src/main/java/com/backend/soullive_a/exception/base/CustomExceptionHandler.java create mode 100644 src/main/java/com/backend/soullive_a/exception/base/ErrorCode.java create mode 100644 src/main/java/com/backend/soullive_a/exception/custom/NotFoundUserException.java diff --git a/src/main/java/com/backend/soullive_a/controller/ExceptionController.java b/src/main/java/com/backend/soullive_a/controller/ExceptionController.java new file mode 100644 index 0000000..374f773 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/controller/ExceptionController.java @@ -0,0 +1,24 @@ +package com.backend.soullive_a.controller; + +import com.backend.soullive_a.exception.base.BaseResponse; +import com.backend.soullive_a.exception.custom.NotFoundUserException; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ExceptionController { + @GetMapping("/success") + public BaseResponse getUser() { + return BaseResponse.builder() + .isSuccess(true) + .code(200) + .message("사용자를 찾았습니다") + .data("somin") + .build(); + } + + @GetMapping("/fail") + public BaseResponse notFoundUser() { + throw new NotFoundUserException(); + } +} \ No newline at end of file diff --git a/src/main/java/com/backend/soullive_a/controller/MainController.java b/src/main/java/com/backend/soullive_a/controller/MainController.java index 1230c14..ca9c58f 100644 --- a/src/main/java/com/backend/soullive_a/controller/MainController.java +++ b/src/main/java/com/backend/soullive_a/controller/MainController.java @@ -10,7 +10,6 @@ public class MainController { public String apiTest() { System.out.println("health-check!"); return "health check! v2."; - } } diff --git a/src/main/java/com/backend/soullive_a/exception/base/BaseException.java b/src/main/java/com/backend/soullive_a/exception/base/BaseException.java new file mode 100644 index 0000000..919e690 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/exception/base/BaseException.java @@ -0,0 +1,26 @@ +package com.backend.soullive_a.exception.base; + +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +public class BaseException extends RuntimeException { + + private final int errorCode; + private final String message; + private final HttpStatus httpStatus; + + public BaseException(ErrorCode errorCode) { + super(); + this.errorCode = errorCode.getCode(); + this.message = errorCode.getMessage(); + this.httpStatus = errorCode.getHttpStatus(); + } + + public BaseException(ErrorCode errorCode, String message) { + super(); + this.errorCode = errorCode.getCode(); + this.message = message; + this.httpStatus = errorCode.getHttpStatus(); + } +} \ No newline at end of file diff --git a/src/main/java/com/backend/soullive_a/exception/base/BaseResponse.java b/src/main/java/com/backend/soullive_a/exception/base/BaseResponse.java new file mode 100644 index 0000000..783301a --- /dev/null +++ b/src/main/java/com/backend/soullive_a/exception/base/BaseResponse.java @@ -0,0 +1,25 @@ +package com.backend.soullive_a.exception.base; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@Getter +@NoArgsConstructor +public class BaseResponse { + + private boolean isSuccess; + private int code; + private String message; + private T data; + + @Builder + public BaseResponse(boolean isSuccess, int code, String message, T data) { + this.isSuccess = isSuccess; + this.code = code; + this.message = message; + this.data = data; + } +} diff --git a/src/main/java/com/backend/soullive_a/exception/base/CustomExceptionHandler.java b/src/main/java/com/backend/soullive_a/exception/base/CustomExceptionHandler.java new file mode 100644 index 0000000..f14a021 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/exception/base/CustomExceptionHandler.java @@ -0,0 +1,52 @@ +package com.backend.soullive_a.exception.base; + +import jakarta.validation.ValidationException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@Slf4j +@RestControllerAdvice +public class CustomExceptionHandler extends ResponseEntityExceptionHandler { + /** + * Custom Exception 핸들링 + */ + @ExceptionHandler(BaseException.class) + public ResponseEntity onBaseException(BaseException exception) { + return new ResponseEntity<>(new BaseResponse<>( + false, + exception.getErrorCode(), + exception.getMessage(), + null), + null, + exception.getHttpStatus()); + } + + /** + * 클라이언트로부터 넘어오는 값 Validation Exception 핸들링 + */ + @ExceptionHandler(ValidationException.class) + public ResponseEntity onValidationException(Exception exception) { + return new ResponseEntity<>(new BaseResponse<>( + false, + 400, + exception.getMessage(), + null), + null, + HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity onException(Exception exception) { + return new ResponseEntity<>(new BaseResponse<>( + false, + 500, + exception.getMessage(), + null), + null, + HttpStatus.INTERNAL_SERVER_ERROR); + } +} diff --git a/src/main/java/com/backend/soullive_a/exception/base/ErrorCode.java b/src/main/java/com/backend/soullive_a/exception/base/ErrorCode.java new file mode 100644 index 0000000..c9572ba --- /dev/null +++ b/src/main/java/com/backend/soullive_a/exception/base/ErrorCode.java @@ -0,0 +1,29 @@ +package com.backend.soullive_a.exception.base; + +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +public enum ErrorCode { + // 성공 + SUCCESS(true, 1000, "요청에 성공했습니다.", HttpStatus.OK), + + //실패 - 필요시 만들어 쓰세요 + /** + * 2000 : User 오류 + */ + NOT_FOUND_USER(false, 2001, "사용자를 찾을 수 없습니다", HttpStatus.NOT_FOUND); + + private final boolean isSuccess; + private final int code; + private final String message; + private final HttpStatus httpStatus; + + ErrorCode(boolean isSuccess, int code, String message, HttpStatus httpStatus) { + this.isSuccess = isSuccess; + this.code = code; + this.message = message; + this.httpStatus = httpStatus; + } +} + diff --git a/src/main/java/com/backend/soullive_a/exception/custom/NotFoundUserException.java b/src/main/java/com/backend/soullive_a/exception/custom/NotFoundUserException.java new file mode 100644 index 0000000..fa3f3a9 --- /dev/null +++ b/src/main/java/com/backend/soullive_a/exception/custom/NotFoundUserException.java @@ -0,0 +1,17 @@ +package com.backend.soullive_a.exception.custom; + +import com.backend.soullive_a.exception.base.BaseException; +import com.backend.soullive_a.exception.base.ErrorCode; +import lombok.Getter; + +@Getter +public class NotFoundUserException extends BaseException { + + public NotFoundUserException() { + super(ErrorCode.NOT_FOUND_USER); + } + + public NotFoundUserException(String message) { + super(ErrorCode.NOT_FOUND_USER, message); + } +} \ No newline at end of file