@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(NoResourceFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseDto<String> handleNoResourceFoundException(NoResourceFoundException e) {
log.error(ERROR_LOG_FORMAT, HttpStatus.NOT_FOUND, e.getMessage(), e);
return new ResponseDto<>(HttpStatus.NOT_FOUND, null, e.getMessage());
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseDto<String> handleException(Exception e) {
log.error(ERROR_LOG_FORMAT, HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e);
return new ResponseDto<>(HttpStatus.INTERNAL_SERVER_ERROR, null, e.getMessage());
}
}
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final String ERROR_LOG_FORMAT = "[{}] {}";
@ExceptionHandler(NoResourceFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseDto<String> handleNoResourceFoundException(NoResourceFoundException e) {
log.error(ERROR_LOG_FORMAT, HttpStatus.NOT_FOUND, e.getMessage(), e);
return new ResponseDto<>(HttpStatus.NOT_FOUND, null, e.getMessage());
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseDto<String> handleException(Exception e) {
log.error(ERROR_LOG_FORMAT, HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e);
return new ResponseDto<>(HttpStatus.INTERNAL_SERVER_ERROR, null, e.getMessage());
}
}
이번 문제에서 배운 점
앞으로 비슷한 문제를 막기 위한 체크리스트