-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: 예외 핸들러 추가 * feature: 예외 처리 추가 * style: spotless apply
- Loading branch information
1 parent
7ed4884
commit 958222c
Showing
5 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/goldbalance/dive/global/exception/CustomException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/main/java/com/goldbalance/dive/global/exception/ErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/goldbalance/dive/global/exception/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/goldbalance/dive/global/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |