Skip to content

Commit b2196c7

Browse files
authored
refactor : 백준 API 호출 시 timeout 에러 처리 추가 (#395)
1 parent b306633 commit b2196c7

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/main/java/com/gamzabat/algohub/feature/problem/service/ProblemService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.scheduling.annotation.Scheduled;
1414
import org.springframework.stereotype.Service;
1515
import org.springframework.transaction.annotation.Transactional;
16+
import org.springframework.web.client.ResourceAccessException;
1617
import org.springframework.web.client.RestTemplate;
1718

1819
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -374,6 +375,10 @@ private JsonNode fetchProblemDetails(String problemId) {
374375
throw new SolvedAcApiErrorException(HttpStatus.BAD_REQUEST.value(), "백준에 유효하지 않은 문제입니다.");
375376

376377
return root.get(0);
378+
} catch (ResourceAccessException e) {
379+
log.error("Timeout or connection failed solved.ac API error : " + e.getMessage());
380+
throw new SolvedAcApiErrorException(HttpStatus.GATEWAY_TIMEOUT.value(),
381+
"solved.ac API 응답이 지연되거나 연결에 실패했습니다. 잠시 후 다시 시도해주세요.");
377382
} catch (JsonProcessingException e) {
378383
log.error("Json processing error : " + e.getMessage());
379384
throw new SolvedAcApiErrorException(HttpStatus.INTERNAL_SERVER_ERROR.value(),

src/test/java/com/gamzabat/algohub/service/ProblemServiceTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.data.domain.Pageable;
2525
import org.springframework.http.HttpStatus;
2626
import org.springframework.http.ResponseEntity;
27+
import org.springframework.web.client.ResourceAccessException;
2728
import org.springframework.web.client.RestTemplate;
2829

2930
import com.gamzabat.algohub.common.DateFormatUtil;
@@ -272,6 +273,29 @@ void createProblemFailed_6() {
272273
.hasFieldOrPropertyWithValue("error", "solved.ac API로부터 예상치 못한 응답을 받았습니다.");
273274
}
274275

276+
@Test
277+
@DisplayName("문제 생성 실패 : solved.ac API 타임아웃 발생")
278+
void createProblemFailed_solvedAcApiTimeout() {
279+
// given
280+
CreateProblemRequest request = CreateProblemRequest.builder()
281+
.link("https://www.acmicpc.net/problem/00")
282+
.startDate(LocalDate.now().minusDays(7))
283+
.endDate(LocalDate.now())
284+
.build();
285+
286+
when(groupRepository.findById(10L)).thenReturn(Optional.of(group));
287+
when(groupMemberRepository.findByUserAndStudyGroup(user, group)).thenReturn(Optional.of(groupMember1));
288+
289+
when(restTemplate.getForEntity(anyString(), eq(String.class)))
290+
.thenThrow(new ResourceAccessException("I/O error: Read timed out"));
291+
292+
// when, then
293+
assertThatThrownBy(() -> problemService.createProblem(user, 10L, request))
294+
.isInstanceOf(SolvedAcApiErrorException.class)
295+
.hasFieldOrPropertyWithValue("code", HttpStatus.GATEWAY_TIMEOUT.value())
296+
.hasFieldOrPropertyWithValue("error", "solved.ac API 응답이 지연되거나 연결에 실패했습니다. 잠시 후 다시 시도해주세요.");
297+
}
298+
275299
@Test
276300
@DisplayName("문제 정보 수정 성공")
277301
void editProblem() {

0 commit comments

Comments
 (0)