Skip to content

Commit

Permalink
feature: 예외 클래스 및 예외 코드 세팅 (#33)
Browse files Browse the repository at this point in the history
* feature: 예외 핸들러 추가

* feature: 예외 처리 추가

* style: spotless apply
  • Loading branch information
Sangwook02 authored Jul 27, 2024
1 parent 7ed4884 commit 958222c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.goldbalance.dive.domain.member.domain.Member;
import com.goldbalance.dive.domain.member.dto.request.MemberSignin;
import com.goldbalance.dive.domain.member.repository.MemberRepository;
import com.goldbalance.dive.global.exception.CustomException;
import com.goldbalance.dive.global.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -20,7 +22,7 @@ public class MemberService {
public void signin(MemberSignin request) {
boolean isDuplicate = memberRepository.existsByNickname(request.nickname());
if (isDuplicate) {
throw new IllegalArgumentException();
throw new CustomException(ErrorCode.MEMBER_NICKNAME_DUPLICATE);
}

Member member = Member.create(request.nickname());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.goldbalance.dive.global.exception;

import lombok.Getter;

@Getter
public class CustomException extends RuntimeException {

private final ErrorCode errorCode;

public CustomException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.errorCode = errorCode;
}

public CustomException(ErrorCode errorCode, String errorMessage) {
super(errorMessage);
this.errorCode = errorCode;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/goldbalance/dive/global/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.goldbalance.dive.global.exception;

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

@Getter
@AllArgsConstructor
public enum ErrorCode {
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러입니다."),

// Member
MEMBER_NICKNAME_DUPLICATE(HttpStatus.BAD_REQUEST, "이미 존재하는 닉네임입니다.");

private final HttpStatus status;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.goldbalance.dive.global.exception;

public record ErrorResponse(String errorCodeName, String errorMessage) {

public static ErrorResponse of(ErrorCode errorCode) {
return new ErrorResponse(errorCode.name(), errorCode.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.goldbalance.dive.global.exception;

import static com.goldbalance.dive.global.exception.ErrorCode.*;

import lombok.extern.slf4j.Slf4j;
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 GlobalExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(CustomException.class)
public ResponseEntity<ErrorResponse> handleCustomException(CustomException e) {
log.error("CustomException : {}", e.getMessage(), e);
return ResponseEntity.status(e.getErrorCode().getStatus()).body(ErrorResponse.of(e.getErrorCode()));
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception e) {
log.error("INTERNAL_SERVER_ERROR : {}", e.getMessage(), e);
return ResponseEntity.status(INTERNAL_SERVER_ERROR.getStatus()).body(ErrorResponse.of(INTERNAL_SERVER_ERROR));
}
}

0 comments on commit 958222c

Please sign in to comment.