Skip to content

Commit

Permalink
feat: #7 예외 발생시 스택 트레이스 출력
Browse files Browse the repository at this point in the history
  • Loading branch information
ehs208 committed Oct 10, 2024
1 parent 654e9a1 commit 1abfed7
Showing 1 changed file with 9 additions and 0 deletions.
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());
}

}

0 comments on commit 1abfed7

Please sign in to comment.