localhost-back/src/main/java/io/company/localhost/common/exception/GlobalExceptionHandler.java

80 lines
3.2 KiB
Java

/************************************************************
*
* @packageName : io.company.localhost.common.exception
* @fileName : GlobalExceptionHandler.java
* @author : 조인제
* @date : 24.12.06
* @description :
*
* ===========================================================
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 24.12.06 조인제 최초 생성
* 25.02.10 서지희 InvalidPasswordException추가
*
*************************************************************/
package io.company.localhost.common.exception;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import io.company.localhost.common.exception.code.CommonErrorCode;
import io.company.localhost.common.exception.code.ErrorCode;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestControllerAdvice(annotations = RestController.class)
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
/**
* 사용자 정의 API 예외 처리 (RestApiException)
*/
@ExceptionHandler(RestApiException.class)
public ResponseEntity<Object> handleCustomException(RestApiException e) {
ErrorCode errorCode = e.getErrorCode();
return ErrorResult.handleExceptionInternal(errorCode);
}
/**
* 잘못된 요청 (IllegalArgumentException) 처리
*/
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<Object> handleIllegalArgument(IllegalArgumentException e) {
log.warn("handleIllegalArgument", e);
ErrorCode errorCode = CommonErrorCode.INVALID_PARAMETER;
return ErrorResult.handleExceptionInternal(errorCode, e.getMessage());
}
/**
* 게시물을 찾을 수 없는 경우 (NotFoundHandler)
*/
@ExceptionHandler(NotFoundHandler.class)
public ResponseEntity<Object> handleBoardNotFoundException(NotFoundHandler e) {
log.warn("handleBoardNotFoundException: {}", e.getMessage());
ErrorCode errorCode = CommonErrorCode.RESOURCE_NOT_FOUND;
return ErrorResult.handleExceptionInternal(errorCode, e.getMessage());
}
/**
* 비밀번호가 틀린 경우 예외 처리 (InvalidPasswordException)
*/
@ExceptionHandler(InvalidPasswordException.class)
public ResponseEntity<Object> handleInvalidPasswordException(InvalidPasswordException e) {
log.warn("handleInvalidPasswordException: {}", e.getMessage());
ErrorCode errorCode = CommonErrorCode.UNAUTHORIZED;
return ErrorResult.handleExceptionInternal(errorCode, e.getMessage());
}
/**
* 서버 내부 오류 처리 (Exception)
*/
@ExceptionHandler({Exception.class})
public ResponseEntity<Object> handleAllException(Exception ex) {
log.warn("handleAllException", ex);
ErrorCode errorCode = CommonErrorCode.INTERNAL_SERVER_ERROR;
return ErrorResult.handleExceptionInternal(errorCode);
}
}