-
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.
- Loading branch information
Showing
1 changed file
with
9 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/main/java/com/leets/xcellentbe/global/error/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 |
---|---|---|
@@ -1,26 +1,35 @@ | ||
package com.leets.xcellentbe.global.error.exception; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import com.leets.xcellentbe.global.error.*; | ||
|
||
@Slf4j | ||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(CommonException.class) // Custom Exception을 포괄적으로 처리 | ||
public ResponseEntity<ErrorResponse> handleCommonException(CommonException ex) { | ||
ErrorCode errorCode = ex.getErrorCode(); // 전달된 예외에서 에러 코드 가져오기 | ||
ErrorResponse errorResponse = new ErrorResponse(errorCode); | ||
showErrorLog(errorCode); | ||
return new ResponseEntity<>(errorResponse, HttpStatus.valueOf(errorCode.getStatus())); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) { | ||
ErrorCode errorCode = ErrorCode.INTERNAL_SERVER_ERROR; | ||
ErrorResponse errorResponse = new ErrorResponse(errorCode); | ||
showErrorLog(errorCode); | ||
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
|
||
private static void showErrorLog(ErrorCode errorCode) { | ||
log.error("errorCode: {}, message: {}", errorCode.getCode(), errorCode.getMessage()); | ||
} | ||
|
||
} |