Skip to content

Commit 30e3e75

Browse files
authored
Merge pull request #128 from TeamPINGLE/fix/127
[fix] 외부 api 호출시 에러처리
2 parents 49ce595 + 92f4cd8 commit 30e3e75

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/main/java/org/pingle/pingleserver/utils/NaverUtil.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ public class NaverUtil {
2828
private String naverLocationSearchUrl;
2929

3030
public List<LocationResponse> getLocationInfo(String location) {
31-
32-
NaverLocationResponse naverLocationResponse = locationSearch(NaverLocationRequest.of(location));
33-
31+
NaverLocationResponse naverLocationResponse;
32+
try {
33+
naverLocationResponse = locationSearch(NaverLocationRequest.of(location));
34+
} catch (RuntimeException e) {
35+
return List.of();
36+
}
3437
return convertResponse(naverLocationResponse);
3538
}
3639

37-
private NaverLocationResponse locationSearch(NaverLocationRequest request) {
40+
private NaverLocationResponse locationSearch(NaverLocationRequest request) throws RuntimeException {
3841
URI uri = UriComponentsBuilder
3942
.fromUriString(naverLocationSearchUrl)
4043
.queryParams(request.toMap())
@@ -50,22 +53,23 @@ private NaverLocationResponse locationSearch(NaverLocationRequest request) {
5053
HttpEntity<Object> httpEntity = new HttpEntity<>(httpHeaders);
5154
ParameterizedTypeReference<NaverLocationResponse> responseType = new ParameterizedTypeReference<>() {
5255
};
53-
54-
ResponseEntity<NaverLocationResponse> responseEntity = new RestTemplate()
55-
.exchange(uri, HttpMethod.GET, httpEntity, responseType);
56-
56+
ResponseEntity<NaverLocationResponse> responseEntity;
57+
58+
try {
59+
responseEntity = new RestTemplate()
60+
.exchange(uri, HttpMethod.GET, httpEntity, responseType);
61+
} catch (Exception e) {
62+
throw new RuntimeException("Naver API 호출 중 오류가 발생했습니다.");
63+
}
5764
return responseEntity.getBody();
5865
}
5966

6067
private List<LocationResponse> convertResponse(NaverLocationResponse response) {
61-
6268
List<NaverLocationResponse.SearchLocationItem> items = response.items();
63-
List<LocationResponse> responseList = items
64-
.stream()
69+
70+
return items.stream()
6571
.map(item -> new LocationResponse(convertX(item.getMapx()), convertY(item.getMapy()), trim(item.getTitle()), trim(item.getAddress()), trim(item.getRoadAddress())))
6672
.collect(Collectors.toList());
67-
68-
return responseList;
6973
}
7074
private double convertX(int x) {
7175

0 commit comments

Comments
 (0)