From b15c196dc036d2452b68e0ffa2b51c5a2b74edfb Mon Sep 17 00:00:00 2001 From: Kim Dae Hwi <121790935+marooo326@users.noreply.github.com> Date: Mon, 8 Jan 2024 10:19:18 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EA=B3=B5=ED=86=B5=20BaseErrorCode?= =?UTF-8?q?=20=EC=A0=95=EC=9D=98=20(#153)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 조은비 <69096886+eunbc@users.noreply.github.com> --- .../pgms/apimember/exception/AdminException.java | 4 +++- .../pgms/apimember/exception/CustomException.java | 6 ++++-- .../apimember/exception/GlobalExceptionHandler.java | 11 ++++------- .../pgms/apimember/exception/MemberException.java | 4 +++- .../pgms/apimember/exception/SecurityException.java | 4 +++- .../com/pgms/apimember/service/AdminService.java | 2 +- .../com/pgms/apimember/service/AuthService.java | 4 ++-- .../com/pgms/apimember/service/MemberService.java | 6 +++--- .../coredomain/domain/common/BaseErrorCode.java | 7 +++++++ .../coredomain/domain/common/MemberErrorCode.java | 13 ++++++++++--- 10 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 core/core-domain/src/main/java/com/pgms/coredomain/domain/common/BaseErrorCode.java rename api/api-member/src/main/java/com/pgms/apimember/exception/CustomErrorCode.java => core/core-domain/src/main/java/com/pgms/coredomain/domain/common/MemberErrorCode.java (87%) diff --git a/api/api-member/src/main/java/com/pgms/apimember/exception/AdminException.java b/api/api-member/src/main/java/com/pgms/apimember/exception/AdminException.java index ccff3677..7cb5a9a0 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/exception/AdminException.java +++ b/api/api-member/src/main/java/com/pgms/apimember/exception/AdminException.java @@ -1,11 +1,13 @@ package com.pgms.apimember.exception; +import com.pgms.coredomain.domain.common.MemberErrorCode; + import lombok.Getter; @Getter public class AdminException extends CustomException { - public AdminException(CustomErrorCode errorCode) { + public AdminException(MemberErrorCode errorCode) { super(errorCode); } } diff --git a/api/api-member/src/main/java/com/pgms/apimember/exception/CustomException.java b/api/api-member/src/main/java/com/pgms/apimember/exception/CustomException.java index 7b0a4a3f..12e46709 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/exception/CustomException.java +++ b/api/api-member/src/main/java/com/pgms/apimember/exception/CustomException.java @@ -1,13 +1,15 @@ package com.pgms.apimember.exception; +import com.pgms.coredomain.domain.common.MemberErrorCode; + import lombok.Getter; @Getter public class CustomException extends RuntimeException { - protected final CustomErrorCode errorCode; + protected final MemberErrorCode errorCode; - public CustomException(CustomErrorCode errorCode) { + public CustomException(MemberErrorCode errorCode) { super(errorCode.getMessage()); this.errorCode = errorCode; } diff --git a/api/api-member/src/main/java/com/pgms/apimember/exception/GlobalExceptionHandler.java b/api/api-member/src/main/java/com/pgms/apimember/exception/GlobalExceptionHandler.java index c296aa1a..08d86b3c 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/exception/GlobalExceptionHandler.java +++ b/api/api-member/src/main/java/com/pgms/apimember/exception/GlobalExceptionHandler.java @@ -1,7 +1,6 @@ package com.pgms.apimember.exception; import java.util.List; -import java.util.Objects; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -11,6 +10,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import com.pgms.coredomain.domain.common.MemberErrorCode; import com.pgms.coredomain.response.ErrorResponse; import lombok.extern.slf4j.Slf4j; @@ -29,20 +29,17 @@ protected ResponseEntity handleGlobalException(Exception ex) { @ExceptionHandler(CustomException.class) protected ResponseEntity handleEventCustomException(CustomException ex) { log.warn(">>>>> Custom Exception : {}", ex); - CustomErrorCode errorCode = ex.getErrorCode(); - ErrorResponse errorResponse = new ErrorResponse(errorCode.getErrorCode(), errorCode.getMessage()); - return ResponseEntity.status(errorCode.getStatus()).body(errorResponse); + MemberErrorCode errorCode = ex.getErrorCode(); + return ResponseEntity.status(errorCode.getStatus()).body(errorCode.getErrorResponse()); } @ExceptionHandler(MethodArgumentNotValidException.class) protected ResponseEntity handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) { log.warn(">>>>> validation Failed : {}", ex); BindingResult bindingResult = ex.getBindingResult(); - String errorMessage = Objects.requireNonNull(bindingResult.getFieldError()) - .getDefaultMessage(); List fieldErrors = bindingResult.getFieldErrors(); - ErrorResponse errorResponse = new ErrorResponse(CustomErrorCode.VALIDATION_FAILED.getErrorCode(), errorMessage); + ErrorResponse errorResponse = MemberErrorCode.VALIDATION_FAILED.getErrorResponse(); fieldErrors.forEach(error -> errorResponse.addValidation(error.getField(), error.getDefaultMessage())); return ResponseEntity.status(ex.getStatusCode()).body(errorResponse); } diff --git a/api/api-member/src/main/java/com/pgms/apimember/exception/MemberException.java b/api/api-member/src/main/java/com/pgms/apimember/exception/MemberException.java index 5b5cf684..75193ece 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/exception/MemberException.java +++ b/api/api-member/src/main/java/com/pgms/apimember/exception/MemberException.java @@ -1,11 +1,13 @@ package com.pgms.apimember.exception; +import com.pgms.coredomain.domain.common.MemberErrorCode; + import lombok.Getter; @Getter public class MemberException extends CustomException { - public MemberException(CustomErrorCode errorCode) { + public MemberException(MemberErrorCode errorCode) { super(errorCode); } } diff --git a/api/api-member/src/main/java/com/pgms/apimember/exception/SecurityException.java b/api/api-member/src/main/java/com/pgms/apimember/exception/SecurityException.java index b80f930b..03a6798d 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/exception/SecurityException.java +++ b/api/api-member/src/main/java/com/pgms/apimember/exception/SecurityException.java @@ -1,7 +1,9 @@ package com.pgms.apimember.exception; +import com.pgms.coredomain.domain.common.MemberErrorCode; + public class SecurityException extends CustomException { - public SecurityException(CustomErrorCode errorCode) { + public SecurityException(MemberErrorCode errorCode) { super(errorCode); } } diff --git a/api/api-member/src/main/java/com/pgms/apimember/service/AdminService.java b/api/api-member/src/main/java/com/pgms/apimember/service/AdminService.java index de3662c2..f8d30f98 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/service/AdminService.java +++ b/api/api-member/src/main/java/com/pgms/apimember/service/AdminService.java @@ -1,6 +1,6 @@ package com.pgms.apimember.service; -import static com.pgms.apimember.exception.CustomErrorCode.*; +import static com.pgms.coredomain.domain.common.MemberErrorCode.*; import java.util.List; diff --git a/api/api-member/src/main/java/com/pgms/apimember/service/AuthService.java b/api/api-member/src/main/java/com/pgms/apimember/service/AuthService.java index 67e55f42..ed6612ae 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/service/AuthService.java +++ b/api/api-member/src/main/java/com/pgms/apimember/service/AuthService.java @@ -10,10 +10,10 @@ import com.pgms.apimember.dto.request.LoginRequest; import com.pgms.apimember.dto.request.RefreshTokenRequest; import com.pgms.apimember.dto.response.AuthResponse; -import com.pgms.apimember.exception.CustomErrorCode; import com.pgms.apimember.exception.SecurityException; import com.pgms.apimember.redis.RefreshToken; import com.pgms.apimember.redis.RefreshTokenRepository; +import com.pgms.coredomain.domain.common.MemberErrorCode; import com.pgms.coresecurity.security.jwt.JwtTokenProvider; import com.pgms.coresecurity.security.service.AdminUserDetailsService; import com.pgms.coresecurity.security.service.MemberUserDetailsService; @@ -57,7 +57,7 @@ public AuthResponse login(LoginRequest request, String accountType) { public AuthResponse refresh(RefreshTokenRequest request) { // refresh token이 만료됐는지 확인 RefreshToken refreshToken = refreshTokenRepository.findById(request.refreshToken()) - .orElseThrow(() -> new SecurityException(CustomErrorCode.REFRESH_TOKEN_EXPIRED)); + .orElseThrow(() -> new SecurityException(MemberErrorCode.REFRESH_TOKEN_EXPIRED)); // 회원 정보 로드 UserDetailsImpl userDetails = loadUserDetails(refreshToken.getAccountType(), refreshToken.getEmail()); diff --git a/api/api-member/src/main/java/com/pgms/apimember/service/MemberService.java b/api/api-member/src/main/java/com/pgms/apimember/service/MemberService.java index d5d9f37c..8e6c2f88 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/service/MemberService.java +++ b/api/api-member/src/main/java/com/pgms/apimember/service/MemberService.java @@ -1,6 +1,6 @@ package com.pgms.apimember.service; -import static com.pgms.apimember.exception.CustomErrorCode.*; +import static com.pgms.coredomain.domain.common.MemberErrorCode.*; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; @@ -10,8 +10,8 @@ import com.pgms.apimember.dto.request.MemberPasswordUpdateRequest; import com.pgms.apimember.dto.request.MemberSignUpRequest; import com.pgms.apimember.dto.response.MemberDetailGetResponse; -import com.pgms.apimember.exception.CustomErrorCode; import com.pgms.apimember.exception.MemberException; +import com.pgms.coredomain.domain.common.MemberErrorCode; import com.pgms.coredomain.domain.member.Member; import com.pgms.coredomain.domain.member.repository.MemberRepository; @@ -40,7 +40,7 @@ public Long signUp(MemberSignUpRequest requestDto) { public MemberDetailGetResponse getMemberDetail(Long memberId) { return MemberDetailGetResponse.from( memberRepository.findById(memberId). - orElseThrow(() -> new MemberException(CustomErrorCode.MEMBER_NOT_FOUND))); + orElseThrow(() -> new MemberException(MemberErrorCode.MEMBER_NOT_FOUND))); } @Transactional(readOnly = true) diff --git a/core/core-domain/src/main/java/com/pgms/coredomain/domain/common/BaseErrorCode.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/common/BaseErrorCode.java new file mode 100644 index 00000000..442af541 --- /dev/null +++ b/core/core-domain/src/main/java/com/pgms/coredomain/domain/common/BaseErrorCode.java @@ -0,0 +1,7 @@ +package com.pgms.coredomain.domain.common; + +import com.pgms.coredomain.response.ErrorResponse; + +public interface BaseErrorCode { + ErrorResponse getErrorResponse(); +} diff --git a/api/api-member/src/main/java/com/pgms/apimember/exception/CustomErrorCode.java b/core/core-domain/src/main/java/com/pgms/coredomain/domain/common/MemberErrorCode.java similarity index 87% rename from api/api-member/src/main/java/com/pgms/apimember/exception/CustomErrorCode.java rename to core/core-domain/src/main/java/com/pgms/coredomain/domain/common/MemberErrorCode.java index 28ed61c1..d5883e35 100644 --- a/api/api-member/src/main/java/com/pgms/apimember/exception/CustomErrorCode.java +++ b/core/core-domain/src/main/java/com/pgms/coredomain/domain/common/MemberErrorCode.java @@ -1,11 +1,13 @@ -package com.pgms.apimember.exception; +package com.pgms.coredomain.domain.common; import org.springframework.http.HttpStatus; +import com.pgms.coredomain.response.ErrorResponse; + import lombok.Getter; @Getter -public enum CustomErrorCode { +public enum MemberErrorCode implements BaseErrorCode { // ADMIN ADMIN_NOT_FOUND("NOT FOUND", HttpStatus.NOT_FOUND, "존재하지 않는 관리자입니다."), DUPLICATED_ADMIN_EMAIL("DUPLICATED ADMIN EMAIL", HttpStatus.BAD_REQUEST, "이미 존재하는 관리자 이메일입니다."), @@ -34,9 +36,14 @@ public enum CustomErrorCode { private final HttpStatus status; private final String message; - CustomErrorCode(String errorCode, HttpStatus status, String message) { + MemberErrorCode(String errorCode, HttpStatus status, String message) { this.errorCode = errorCode; this.status = status; this.message = message; } + + @Override + public ErrorResponse getErrorResponse() { + return new ErrorResponse(errorCode, message); + } }