From 227a90c59c097cee1a499e74e2fdcbc71d6583c4 Mon Sep 17 00:00:00 2001 From: yummygyudon Date: Fri, 24 Nov 2023 23:47:27 +0900 Subject: [PATCH 1/6] =?UTF-8?q?FEAT=20:=20=EA=B3=84=EC=B8=B5=20=EA=B5=AC?= =?UTF-8?q?=EC=A1=B0=20=EA=B8=B0=EB=B0=98=20=EC=9D=B8=ED=84=B0=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=20=EB=B0=8F=20Base=20Exception=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/exception/base/ErrorBase.java | 10 +++++++++ .../common/exception/base/ExceptionBase.java | 21 +++++++++++++++++++ .../common/exception/base/RootEnum.java | 4 ++++ .../common/exception/base/SuccessBase.java | 11 ++++++++++ 4 files changed, 46 insertions(+) create mode 100644 src/main/java/org/sopt/sopkerton/common/exception/base/ErrorBase.java create mode 100644 src/main/java/org/sopt/sopkerton/common/exception/base/ExceptionBase.java create mode 100644 src/main/java/org/sopt/sopkerton/common/exception/base/RootEnum.java create mode 100644 src/main/java/org/sopt/sopkerton/common/exception/base/SuccessBase.java diff --git a/src/main/java/org/sopt/sopkerton/common/exception/base/ErrorBase.java b/src/main/java/org/sopt/sopkerton/common/exception/base/ErrorBase.java new file mode 100644 index 0000000..3b340d9 --- /dev/null +++ b/src/main/java/org/sopt/sopkerton/common/exception/base/ErrorBase.java @@ -0,0 +1,10 @@ +package org.sopt.sopkerton.common.exception.base; + +import org.springframework.http.HttpStatus; + +public interface ErrorBase extends RootEnum{ + int getHttpStatusCode(); + + HttpStatus getHttpStatus(); + String getErrorMessage(); +} diff --git a/src/main/java/org/sopt/sopkerton/common/exception/base/ExceptionBase.java b/src/main/java/org/sopt/sopkerton/common/exception/base/ExceptionBase.java new file mode 100644 index 0000000..6a5184e --- /dev/null +++ b/src/main/java/org/sopt/sopkerton/common/exception/base/ExceptionBase.java @@ -0,0 +1,21 @@ +package org.sopt.sopkerton.common.exception.base; + +import lombok.Getter; +import org.springframework.http.HttpStatus; + +/** + * == 하위 도메인 예외는 본 Exception 클래스를 상속 받습니다. == + */ +@Getter +public class ExceptionBase extends RuntimeException{ + private static final String ERROR_MESSAGE_HEADER = "ERROR : "; + + private final ErrorBase errorBase; + public ExceptionBase(ErrorBase errorBase) { + super(ERROR_MESSAGE_HEADER + errorBase.getErrorMessage()); + this.errorBase = errorBase; + } + public HttpStatus getStatus() { + return errorBase.getHttpStatus(); + } +} diff --git a/src/main/java/org/sopt/sopkerton/common/exception/base/RootEnum.java b/src/main/java/org/sopt/sopkerton/common/exception/base/RootEnum.java new file mode 100644 index 0000000..4f6fca7 --- /dev/null +++ b/src/main/java/org/sopt/sopkerton/common/exception/base/RootEnum.java @@ -0,0 +1,4 @@ +package org.sopt.sopkerton.common.exception.base; + +public interface RootEnum { +} diff --git a/src/main/java/org/sopt/sopkerton/common/exception/base/SuccessBase.java b/src/main/java/org/sopt/sopkerton/common/exception/base/SuccessBase.java new file mode 100644 index 0000000..bea0d30 --- /dev/null +++ b/src/main/java/org/sopt/sopkerton/common/exception/base/SuccessBase.java @@ -0,0 +1,11 @@ +package org.sopt.sopkerton.common.exception.base; + +import org.springframework.http.HttpStatus; + +public interface SuccessBase extends RootEnum{ + int getHttpStatusCode(); + + HttpStatus getHttpStatus(); + String getSuccessMessage(); + +} From 99b4373a7592a83ec265ca32b8726068214def93 Mon Sep 17 00:00:00 2001 From: yummygyudon Date: Fri, 24 Nov 2023 23:48:15 +0900 Subject: [PATCH 2/6] =?UTF-8?q?FEAT=20:=20=EC=A0=84=EC=97=AD=EC=A0=81/?= =?UTF-8?q?=EC=8B=9C=EC=8A=A4=ED=85=9C=EC=A0=81(Global)=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=ED=98=B9=EC=9D=80=20=EC=84=B1?= =?UTF-8?q?=EA=B3=B5=20=EB=B0=98=ED=99=98=20=EC=8B=9C=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=EB=90=A0=20=EA=B0=9D=EC=B2=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/exception/GlobalError.java | 32 +++++++++++++++++++ .../common/exception/GlobalException.java | 13 ++++++++ .../common/exception/GlobalSuccess.java | 30 +++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/main/java/org/sopt/sopkerton/common/exception/GlobalError.java create mode 100644 src/main/java/org/sopt/sopkerton/common/exception/GlobalException.java create mode 100644 src/main/java/org/sopt/sopkerton/common/exception/GlobalSuccess.java diff --git a/src/main/java/org/sopt/sopkerton/common/exception/GlobalError.java b/src/main/java/org/sopt/sopkerton/common/exception/GlobalError.java new file mode 100644 index 0000000..daf893f --- /dev/null +++ b/src/main/java/org/sopt/sopkerton/common/exception/GlobalError.java @@ -0,0 +1,32 @@ +package org.sopt.sopkerton.common.exception; + +import lombok.AllArgsConstructor; +import org.sopt.sopkerton.common.exception.base.ErrorBase; +import org.springframework.http.HttpStatus; + +@AllArgsConstructor +public enum GlobalError implements ErrorBase { + + ; + + private final HttpStatus status; + private final String errorMessage; + + + @Override + public int getHttpStatusCode() { + return status.value(); + } + + @Override + public HttpStatus getHttpStatus() { + return this.status; + } + + + @Override + public String getErrorMessage() { + return this.errorMessage; + } + +} diff --git a/src/main/java/org/sopt/sopkerton/common/exception/GlobalException.java b/src/main/java/org/sopt/sopkerton/common/exception/GlobalException.java new file mode 100644 index 0000000..11f96be --- /dev/null +++ b/src/main/java/org/sopt/sopkerton/common/exception/GlobalException.java @@ -0,0 +1,13 @@ +package org.sopt.sopkerton.common.exception; + +import org.sopt.sopkerton.common.exception.base.ExceptionBase; + +/** + * 전역적 혹은 시스템 전체 범위에서 발생하는 예외 + */ +public class GlobalException extends ExceptionBase { + + public GlobalException(GlobalError error) { + super(error); + } +} diff --git a/src/main/java/org/sopt/sopkerton/common/exception/GlobalSuccess.java b/src/main/java/org/sopt/sopkerton/common/exception/GlobalSuccess.java new file mode 100644 index 0000000..6852d5b --- /dev/null +++ b/src/main/java/org/sopt/sopkerton/common/exception/GlobalSuccess.java @@ -0,0 +1,30 @@ +package org.sopt.sopkerton.common.exception; + +import lombok.AllArgsConstructor; +import org.sopt.sopkerton.common.exception.base.SuccessBase; +import org.springframework.http.HttpStatus; + +@AllArgsConstructor +public enum GlobalSuccess implements SuccessBase { + ; + + private final HttpStatus status; + private final String successMessage; + + + @Override + public int getHttpStatusCode() { + return this.status.value(); + } + + @Override + public HttpStatus getHttpStatus() { + return this.status; + } + + @Override + public String getSuccessMessage() { + return this.successMessage; + } + +} From a5da34c88fb5c85e3b9e4de54f53a847d1b1fa0d Mon Sep 17 00:00:00 2001 From: yummygyudon Date: Fri, 24 Nov 2023 23:48:54 +0900 Subject: [PATCH 3/6] =?UTF-8?q?FEAT=20:=20Response=20DTO=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Thanks to 동섭 & 승환 --- .../common/response/ApiResponse.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/main/java/org/sopt/sopkerton/common/response/ApiResponse.java diff --git a/src/main/java/org/sopt/sopkerton/common/response/ApiResponse.java b/src/main/java/org/sopt/sopkerton/common/response/ApiResponse.java new file mode 100644 index 0000000..a64d7d3 --- /dev/null +++ b/src/main/java/org/sopt/sopkerton/common/response/ApiResponse.java @@ -0,0 +1,38 @@ +package org.sopt.sopkerton.common.response; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; +import org.sopt.sopkerton.common.exception.base.ErrorBase; +import org.sopt.sopkerton.common.exception.base.SuccessBase; + +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public class ApiResponse { + private final int code; + private final String message; + + @JsonInclude(JsonInclude.Include.NON_NULL) + private T data; + + public static ApiResponse success(SuccessBase success) { + return new ApiResponse<>(success.getHttpStatusCode(), success.getSuccessMessage()); + } + + public static ApiResponse success(SuccessBase success, T data) { + return new ApiResponse(success.getHttpStatusCode(), success.getSuccessMessage(), data); + } + + public static ApiResponse error(ErrorBase error) { + return new ApiResponse<>(error.getHttpStatusCode(), error.getErrorMessage()); + } + + public static ApiResponse error(ErrorBase error, T data) { + return new ApiResponse<>(error.getHttpStatusCode(), error.getErrorMessage(), data); + } + + public static ApiResponse error(ErrorBase error, Exception exception) { + return new ApiResponse<>(error.getHttpStatusCode(), error.getErrorMessage(), exception); + } +} From 8f055dadf08aa4a2f01c216f315c3313f6e26487 Mon Sep 17 00:00:00 2001 From: yummygyudon Date: Fri, 24 Nov 2023 23:49:43 +0900 Subject: [PATCH 4/6] =?UTF-8?q?FEAT=20:=20=EB=B9=84=EC=A6=88=EB=8B=88?= =?UTF-8?q?=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20=EB=82=B4=20Runtime=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=EB=B0=9C=EC=83=9D=EC=97=90=20=EB=8C=80?= =?UTF-8?q?=ED=95=9C=20Exception=20Handler=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../response/CommonControllerAdvice.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/java/org/sopt/sopkerton/common/response/CommonControllerAdvice.java diff --git a/src/main/java/org/sopt/sopkerton/common/response/CommonControllerAdvice.java b/src/main/java/org/sopt/sopkerton/common/response/CommonControllerAdvice.java new file mode 100644 index 0000000..cbe91ce --- /dev/null +++ b/src/main/java/org/sopt/sopkerton/common/response/CommonControllerAdvice.java @@ -0,0 +1,24 @@ +package org.sopt.sopkerton.common.response; + +import lombok.RequiredArgsConstructor; + +import org.sopt.sopkerton.common.exception.base.ExceptionBase; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +@RequiredArgsConstructor +public class CommonControllerAdvice { + + @ExceptionHandler(value = ExceptionBase.class) + public ResponseEntity sopkathonExceptionHandler(ExceptionBase exception) { + return ResponseEntity + .status(exception.getStatus()) + .body( + ApiResponse.error(exception.getErrorBase(), exception) + ); + } + +} From 56d321432289a77a2e28218a8253973b2569b13b Mon Sep 17 00:00:00 2001 From: yummygyudon Date: Sat, 25 Nov 2023 03:10:38 +0900 Subject: [PATCH 5/6] =?UTF-8?q?FIX=20:=20=EB=B6=80=EB=AA=A8=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EB=A1=9C=EB=A7=8C=20=EC=93=B0=EC=97=AC?= =?UTF-8?q?=EC=95=BC=20=ED=95=98=EA=B8=B0=20=EB=95=8C=EB=AC=B8=EC=97=90=20?= =?UTF-8?q?=EC=B6=94=EC=83=81=ED=81=B4=EB=9E=98=EC=8A=A4=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/exception/base/ExceptionBase.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/sopt/sopkerton/common/exception/base/ExceptionBase.java b/src/main/java/org/sopt/sopkerton/common/exception/base/ExceptionBase.java index 6a5184e..014e7a2 100644 --- a/src/main/java/org/sopt/sopkerton/common/exception/base/ExceptionBase.java +++ b/src/main/java/org/sopt/sopkerton/common/exception/base/ExceptionBase.java @@ -1,21 +1,20 @@ package org.sopt.sopkerton.common.exception.base; -import lombok.Getter; -import org.springframework.http.HttpStatus; /** - * == 하위 도메인 예외는 본 Exception 클래스를 상속 받습니다. == + * 하위 도메인 예외는 본 Exception 클래스를 상속 받습니다. + * - 객체 생 */ -@Getter -public class ExceptionBase extends RuntimeException{ +public abstract class ExceptionBase extends RuntimeException{ private static final String ERROR_MESSAGE_HEADER = "ERROR : "; private final ErrorBase errorBase; - public ExceptionBase(ErrorBase errorBase) { + protected ExceptionBase(ErrorBase errorBase) { super(ERROR_MESSAGE_HEADER + errorBase.getErrorMessage()); this.errorBase = errorBase; } - public HttpStatus getStatus() { - return errorBase.getHttpStatus(); + + public ErrorBase getError() { + return errorBase; } } From 95b6514c672c322bfa71ef802668ddd95c52e7bb Mon Sep 17 00:00:00 2001 From: yummygyudon Date: Sat, 25 Nov 2023 03:11:11 +0900 Subject: [PATCH 6/6] =?UTF-8?q?FIX=20:=20Getter=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=9D=B4=EB=A6=84=20=EB=B0=8F=20=EB=B6=80=EB=AA=A8?= =?UTF-8?q?=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=ED=95=84=EB=93=9C=20=EA=B5=AC?= =?UTF-8?q?=EC=A1=B0=20=EB=B3=80=EA=B2=BD=EC=97=90=20=EB=94=B0=EB=A5=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sopkerton/common/response/CommonControllerAdvice.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/sopt/sopkerton/common/response/CommonControllerAdvice.java b/src/main/java/org/sopt/sopkerton/common/response/CommonControllerAdvice.java index cbe91ce..09324c4 100644 --- a/src/main/java/org/sopt/sopkerton/common/response/CommonControllerAdvice.java +++ b/src/main/java/org/sopt/sopkerton/common/response/CommonControllerAdvice.java @@ -2,6 +2,7 @@ import lombok.RequiredArgsConstructor; +import org.sopt.sopkerton.common.exception.base.ErrorBase; import org.sopt.sopkerton.common.exception.base.ExceptionBase; import org.springframework.http.ResponseEntity; @@ -14,10 +15,11 @@ public class CommonControllerAdvice { @ExceptionHandler(value = ExceptionBase.class) public ResponseEntity sopkathonExceptionHandler(ExceptionBase exception) { + ErrorBase error = exception.getError(); return ResponseEntity - .status(exception.getStatus()) + .status(error.getHttpStatus()) .body( - ApiResponse.error(exception.getErrorBase(), exception) + ApiResponse.error(error) ); }