Skip to content

Commit

Permalink
[REFACTOR] 크롤러 예외 처리 추가 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
unluckyboy214 authored Oct 26, 2024
1 parent c8d2414 commit a980f9f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/catspot/crawler/CrawlerScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public void crawl() {
List<StudyPlace> places = LibraryCrawler.getData();
List<StudyPlace> studyPlacesToSave = new ArrayList<>();

if (places.isEmpty()) {
studyPlaceRepository.deleteAll();
return;
}

for (StudyPlace place : places) {
StudyPlace studyPlace = studyPlaceRepository.findById(place.getPlaceIdx()).orElseGet(() -> place);

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/catspot/crawler/LibraryCrawler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.catspot.crawler;

import com.catspot.exceptionhandler.CommonErrorCode;
import com.catspot.exceptionhandler.CustomException;
import com.catspot.exceptionhandler.ErrorCode;
import com.catspot.studyplace.StudyPlace;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
Expand All @@ -9,6 +13,7 @@
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class LibraryCrawler {
private static final String baseUrl = "http://203.229.203.240/8080";
private static final String tableUrl = "/Domian5_jythh.asp";
Expand Down Expand Up @@ -44,9 +49,10 @@ public static List<StudyPlace> getData() {
data.add(studyPlace);
}

if (data.isEmpty()) throw new IOException();
} catch (IOException e) {
e.printStackTrace();

} catch (Exception e) {
log.error("크롤링에 실패했습니다.");
data = new ArrayList<>();
}
return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum CommonErrorCode implements ErrorCode{
INVALID_PARAMETER(HttpStatus.BAD_REQUEST, "Invalid parameter included"),
RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "Resource not exists"),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Internal server error"),
NO_DATA_FOUND(HttpStatus.NOT_FOUND, "No data available")
;
private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.util.ArrayList;
import java.util.List;

import com.catspot.exceptionhandler.CommonErrorCode;
import com.catspot.exceptionhandler.CustomException;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -16,6 +19,11 @@ public class StudyPlaceController {
@GetMapping("/study-seat")
public StudyPlaceResponse getAllStudyPlaces() {
List<StudyPlace> allStudyPlaces = studyPlaceRepository.findAll();

if(allStudyPlaces.isEmpty()){
throw new CustomException(CommonErrorCode.NO_DATA_FOUND);
}

List<StudyPlaceDto> studyPlaceDtos = new ArrayList<>();

for (StudyPlace studyPlace : allStudyPlaces) {
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/catspot/crawler/LibraryCrawlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ class LibraryCrawlerTest {
System.out.println(data);
Assertions.assertFalse(data.isEmpty());
}

}

0 comments on commit a980f9f

Please sign in to comment.