Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTOR]/#86 - Exception 구조 변경 #98

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'

// reids
// redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis:2.3.1.RELEASE'

// lombok
Expand All @@ -56,6 +56,9 @@ dependencies {
// jackson
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.8'

// validation
implementation 'org.springframework.boot:spring-boot-starter-validation'

runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.example.tree.domain.profile.service.ProfileService;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;
import org.example.tree.domain.reaction.service.ReactionService;
import org.example.tree.global.exception.AuthErrorCode;
import org.example.tree.global.exception.AuthException;
import org.example.tree.global.exception.GeneralException;
import org.example.tree.global.exception.GlobalErrorCode;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -66,7 +68,7 @@ public void updateComment(Long treeId, Long postId, Long commentId, CommentReque
Post post = postQueryService.findById(postId);
Comment comment = commentQueryService.findById(commentId);
if (!comment.getProfile().getId().equals(profile.getId())) {
throw new GeneralException(GlobalErrorCode.AUTHENTICATION_REQUIRED);
throw new AuthException(AuthErrorCode.AUTHENTICATION_REQUIRED);
}
comment.updateComment(request.getContent());
}
Expand All @@ -77,7 +79,7 @@ public void deleteComment(Long treeId, Long postId, Long commentId, Member membe
Post post = postQueryService.findById(postId);
Comment comment = commentQueryService.findById(commentId);
if (!comment.getProfile().getId().equals(profile.getId())) {
throw new GeneralException(GlobalErrorCode.AUTHENTICATION_REQUIRED);
throw new AuthException(AuthErrorCode.AUTHENTICATION_REQUIRED);
}
post.decreaseCommentCount();
commentCommandService.deleteComment(comment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.example.tree.domain.profile.service.ProfileService;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;
import org.example.tree.domain.reaction.service.ReactionService;
import org.example.tree.global.exception.AuthErrorCode;
import org.example.tree.global.exception.AuthException;
import org.example.tree.global.exception.GeneralException;
import org.example.tree.global.exception.GlobalErrorCode;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -54,7 +56,7 @@ public void updateReply(Long treeId, Long commentId, Long replyId, ReplyRequestD
Comment comment = commentQueryService.findById(commentId);
Reply reply = replyQueryService.findById(replyId);
if (!reply.getProfile().getId().equals(profile.getId())) {
throw new GeneralException(GlobalErrorCode.AUTHENTICATION_REQUIRED);
throw new AuthException(AuthErrorCode.AUTHENTICATION_REQUIRED);
}
reply.updateReply(request.getContent());
}
Expand All @@ -65,7 +67,7 @@ public void deleteReply(Long treeId, Long commentId, Long replyId, Member member
Comment comment = commentQueryService.findById(commentId);
Reply reply = replyQueryService.findById(replyId);
if (!reply.getProfile().getId().equals(profile.getId())) {
throw new GeneralException(GlobalErrorCode.AUTHENTICATION_REQUIRED);
throw new AuthException(AuthErrorCode.AUTHENTICATION_REQUIRED);
}
replyCommandService.deleteReply(reply);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.example.tree.domain.member.dto.MemberResponseDTO;
import org.example.tree.domain.member.entity.Member;
import org.example.tree.domain.member.entity.redis.RefreshToken;
import org.example.tree.global.exception.AuthErrorCode;
import org.example.tree.global.exception.AuthException;
import org.example.tree.global.exception.GeneralException;
import org.example.tree.global.exception.GlobalErrorCode;
import org.example.tree.global.redis.service.RedisService;
Expand Down Expand Up @@ -47,7 +49,7 @@ public MemberResponseDTO.registerMember login(MemberRequestDTO.loginMember reque

@Transactional
public MemberResponseDTO.reissue reissue(MemberRequestDTO.reissue request) {
RefreshToken refreshToken = redisService.findRefreshToken(request.getRefreshToken()).orElseThrow(() -> new GeneralException(GlobalErrorCode.REFRESH_TOKEN_EXPIRED));
RefreshToken refreshToken = redisService.findRefreshToken(request.getRefreshToken()).orElseThrow(() -> new AuthException(AuthErrorCode.REFRESH_TOKEN_EXPIRED));
Member member = memberQueryService.findById(refreshToken.getMemberId());
TokenDTO token = memberCommandService.reissueToken(member,refreshToken);
return memberConverter.toReissue(token.getAccessToken(), token.getRefreshToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.example.tree.domain.profile.service.ProfileService;
import org.example.tree.domain.reaction.dto.ReactionResponseDTO;
import org.example.tree.domain.reaction.service.ReactionService;
import org.example.tree.global.exception.AuthErrorCode;
import org.example.tree.global.exception.AuthException;
import org.example.tree.global.exception.GeneralException;
import org.example.tree.global.exception.GlobalErrorCode;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -93,7 +95,7 @@ public void updatePost(Long treeId, Long postId, PostRequestDTO.updatePost reque
Profile profile = profileService.getTreeProfile(member, treeId);
Post post = postQueryService.findById(postId);
if (!post.getProfile().getId().equals(profile.getId())) {
throw new GeneralException(GlobalErrorCode.AUTHENTICATION_REQUIRED);
throw new AuthException(AuthErrorCode.AUTHENTICATION_REQUIRED);
}
post.updatePost(request.getContent());
}
Expand All @@ -103,7 +105,7 @@ public void deletePost(Long treeId, Long postId, Member member) {
Profile profile = profileService.getTreeProfile(member, treeId);
Post post = postQueryService.findById(postId);
if (!post.getProfile().getId().equals(profile.getId())) {
throw new GeneralException(GlobalErrorCode.AUTHENTICATION_REQUIRED);
throw new AuthException(AuthErrorCode.AUTHENTICATION_REQUIRED);
}
postCommandService.deletePost(post);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TreeQueryService {

public Tree findById(Long id) {
return treeRepository.findById(id)
.orElseThrow(()->new GeneralException(GlobalErrorCode.TREE_NOT_FOUND));
.orElseThrow(()->new GeneralException(GlobalErrorCode.TREEHOUSE_NOT_FOUND));
}

}
4 changes: 2 additions & 2 deletions src/main/java/org/example/tree/global/common/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public static <T> ApiResponse<T> onSuccess(T data){
}

// 실패한 경우 응답 생성
public static <T> ApiResponse<T> onFailure(GlobalErrorCode code, T data){
return new ApiResponse<>(false, String.valueOf(code.getHttpStatus().value()), code.getMessage(), LocalDateTime.now(), data);
public static <T> ApiResponse<T> onFailure(String code, String message, T data){
return new ApiResponse<>(false,code, message, LocalDateTime.now(), data);
}

}
49 changes: 49 additions & 0 deletions src/main/java/org/example/tree/global/exception/AuthErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.example.tree.global.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.*;

@Getter
@AllArgsConstructor
public enum AuthErrorCode implements BaseErrorCode{
// 401 Unauthorized - 권한 없음
TOKEN_EXPIRED(UNAUTHORIZED, "AUTH401_1", "인증 토큰이 만료 되었습니다. 토큰을 재발급 해주세요"),
INVALID_TOKEN(UNAUTHORIZED, "AUTH401_2", "인증 토큰이 유효하지 않습니다."),
INVALID_REFRESH_TOKEN(UNAUTHORIZED, "AUTH401_3", "리프레시 토큰이 유효하지 않습니다."),
REFRESH_TOKEN_EXPIRED(UNAUTHORIZED, "AUTH401_4", "리프레시 토큰이 만료 되었습니다."),
AUTHENTICATION_REQUIRED(UNAUTHORIZED, "AUTH401_5", "인증 정보가 유효하지 않습니다."),
LOGIN_REQUIRED(UNAUTHORIZED, "AUTH401_6", "로그인이 필요한 서비스입니다."),

// 403 Forbidden - 인증 거부
AUTHENTICATION_DENIED(FORBIDDEN, "AUTH403_1", "인증이 거부 되었습니다."),

// 404 Not Found - 찾을 수 없음
REFRESH_TOKEN_NOT_FOUND(NOT_FOUND, "AUTH404_1", "리프레시 토큰이 존재하지 않습니다."),
;

private final HttpStatus httpStatus;
private final String code;
private final String message;

@Override
public ErrorReasonDTO getReason() {
return ErrorReasonDTO.builder()
.message(message)
.code(code)
.isSuccess(false)
.build();
}

@Override
public ErrorReasonDTO getReasonHttpStatus() {
return ErrorReasonDTO.builder()
.message(message)
.code(code)
.httpStatus(httpStatus)
.isSuccess(false)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.example.tree.global.exception;

public class AuthException extends GeneralException{

public AuthException(BaseErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.example.tree.global.exception;

public interface BaseErrorCode {

public ErrorReasonDTO getReason();

public ErrorReasonDTO getReasonHttpStatus();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.example.tree.global.exception;

import lombok.Builder;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@Builder
public class ErrorReasonDTO {

private HttpStatus httpStatus;

private final boolean isSuccess;
private final String code;
private final String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@
@Getter
@AllArgsConstructor
public class GeneralException extends RuntimeException {
private final GlobalErrorCode errorCode;

private final BaseErrorCode errorCode;

public ErrorReasonDTO getErrorReason() {
return this.errorCode.getReason();
}

public ErrorReasonDTO getErrorReasonHttpStatus() {
return this.errorCode.getReasonHttpStatus();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,22 @@ public ResponseEntity onThrowException(
GeneralException generalException,
@AuthenticationPrincipal User user,
HttpServletRequest request) {
getExceptionStackTrace(generalException, user, request);
GlobalErrorCode errorCode = generalException.getErrorCode();
return handleExceptionInternal(generalException, errorCode, null, request);
// getExceptionStackTrace(generalException, user, request);
// GlobalErrorCode errorCode = generalException.getErrorCode();
ErrorReasonDTO errorReasonHttpStatus = generalException.getErrorReasonHttpStatus();
return handleExceptionInternal(generalException, errorReasonHttpStatus, null, request);
}

private ResponseEntity<Object> handleExceptionInternal(
Exception e, GlobalErrorCode errorCode, HttpHeaders headers, HttpServletRequest request) {
Exception e, ErrorReasonDTO reason, HttpHeaders headers, HttpServletRequest request) {

ApiResponse<Object> body =
ApiResponse.onFailure(errorCode, null);
ApiResponse.onFailure(reason.getCode(), reason.getMessage(), null);
e.printStackTrace();

WebRequest webRequest = new ServletWebRequest(request);
return super.handleExceptionInternal(
e, body, headers, errorCode.getHttpStatus(), webRequest);
e, body, headers, reason.getHttpStatus(), webRequest);
}

private ResponseEntity<Object> handleExceptionInternalFalse(
Expand All @@ -121,7 +122,7 @@ private ResponseEntity<Object> handleExceptionInternalFalse(
WebRequest request,
String errorPoint) {
ApiResponse<Object> body =
ApiResponse.onFailure(errorCode, errorPoint);
ApiResponse.onFailure(errorCode.getCode(), errorCode.getMessage(), errorPoint);
return super.handleExceptionInternal(e, body, headers, status, request);
}

Expand All @@ -132,14 +133,14 @@ private ResponseEntity<Object> handleExceptionInternalArgs(
WebRequest request,
Map<String, String> errorArgs) {
ApiResponse<Object> body =
ApiResponse.onFailure(errorCode, errorArgs);
ApiResponse.onFailure(errorCode.getCode(), errorCode.getMessage(), errorArgs);
return super.handleExceptionInternal(e, body, headers, errorCode.getHttpStatus(), request);
}

private ResponseEntity<Object> handleExceptionInternalConstraint(
Exception e, GlobalErrorCode errorCode, HttpHeaders headers, WebRequest request) {
ApiResponse<Object> body =
ApiResponse.onFailure(errorCode, null);
ApiResponse.onFailure(errorCode.getCode(), errorCode.getMessage(), null);
return super.handleExceptionInternal(e, body, headers, errorCode.getHttpStatus(), request);
}

Expand Down
Loading
Loading