From 9f9854917a52671c512c48089c9a3546b54ab0b9 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 16:47:45 +0900 Subject: [PATCH] fix: collect sentry errors --- .../vouchers/service/VoucherSaveService.java | 52 ++++++++----------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java index 86217b24..47a1e0a2 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java @@ -8,13 +8,12 @@ import org.swmaestro.repl.gifthub.auth.service.UserService; import org.swmaestro.repl.gifthub.exception.BusinessException; import org.swmaestro.repl.gifthub.exception.GptResponseException; -import org.swmaestro.repl.gifthub.exception.TimeoutException; +import org.swmaestro.repl.gifthub.exception.GptTimeoutException; import org.swmaestro.repl.gifthub.notifications.NotificationType; import org.swmaestro.repl.gifthub.notifications.service.FCMNotificationService; import org.swmaestro.repl.gifthub.notifications.service.NotificationService; import org.swmaestro.repl.gifthub.util.ProductNameProcessor; import org.swmaestro.repl.gifthub.util.QueryTemplateReader; -import org.swmaestro.repl.gifthub.util.StatusEnum; import org.swmaestro.repl.gifthub.vouchers.dto.GptResponseDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherAutoSaveRequestDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherSaveRequestDto; @@ -23,6 +22,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import io.sentry.Sentry; import lombok.RequiredArgsConstructor; import reactor.core.publisher.Mono; @@ -42,8 +42,7 @@ public class VoucherSaveService { public void execute(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto, String username, Long pendingId) throws IOException { String filename = voucherAutoSaveRequestDto.getFilename(); - handleGptResponse(voucherAutoSaveRequestDto, username) - .flatMap(voucherSaveRequestDto -> handleSearchResponse(voucherSaveRequestDto, username, filename)) + handleGptResponse(voucherAutoSaveRequestDto, username).flatMap(voucherSaveRequestDto -> handleSearchResponse(voucherSaveRequestDto, username, filename)) .flatMap(voucherSaveRequestDto -> handleVoucherSaving(voucherSaveRequestDto, username)) .subscribe( // onSuccess @@ -55,56 +54,49 @@ public void execute(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto, String if (voucherService.read(voucherSaveResponseDto.getId()).getExpiresAt().isBefore(LocalDate.now())) { fcmNotificationService.sendNotification("기프티콘 등록 성공", "만료된 기프티콘을 등록했습니다.", username); notificationService.save(userService.read(username), voucherService.read(voucherSaveResponseDto.getId()), - NotificationType.REGISTERED, - "만료된 기프티콘을 등록했습니다."); + NotificationType.REGISTERED, "만료된 기프티콘을 등록했습니다."); } else { fcmNotificationService.sendNotification("기프티콘 등록 성공", "기프티콘 등록에 성공했습니다!", username); notificationService.save(userService.read(username), voucherService.read(voucherSaveResponseDto.getId()), - NotificationType.REGISTERED, - "기프티콘 등록에 성공했습니다."); + NotificationType.REGISTERED, "기프티콘 등록에 성공했습니다."); } }, // onError throwable -> { System.out.println("등록 실패"); + Sentry.captureException(throwable); // 처리 완료 pendingVoucherService.delete(pendingId); throwable.printStackTrace(); if (throwable instanceof BusinessException) { fcmNotificationService.sendNotification("기프티콘 등록 실패", "이미 등록된 기프티콘 입니다.", username); - notificationService.save(userService.read(username), null, - NotificationType.REGISTERED, - "이미 등록된 기프티콘 입니다."); + notificationService.save(userService.read(username), null, NotificationType.REGISTERED, "이미 등록된 기프티콘 입니다."); } else { fcmNotificationService.sendNotification("기프티콘 등록 실패", "자동 등록에 실패했습니다. 수동 등록을 이용해 주세요.", username); - notificationService.save(userService.read(username), null, - NotificationType.REGISTERED, - "자동 등록에 실패했습니다. 수동 등록을 이용해 주세요."); + notificationService.save(userService.read(username), null, NotificationType.REGISTERED, "자동 등록에 실패했습니다. 수동 등록을 이용해 주세요."); } }); } public Mono handleGptResponse(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto, String username) throws - IOException, GptResponseException, - TimeoutException { + GptTimeoutException { + return gptService.getGptResponse(voucherAutoSaveRequestDto) - .timeout(Duration.ofSeconds(15)) - .onErrorResume(TimeoutException.class, throwable -> Mono.error(new TimeoutException("GPT 요청이 시간초과되었습니다.", StatusEnum.NOT_FOUND))) + .timeout(Duration.ofMinutes(15)) + .onErrorResume(GptTimeoutException.class, throwable -> Mono.error(new GptTimeoutException())) .flatMap(response -> { + VoucherSaveRequestDto voucherSaveRequestDto = null; try { - VoucherSaveRequestDto voucherSaveRequestDto = createVoucherSaveRequestDto(response); - if (voucherSaveRequestDto.getBrandName() == "" || - voucherSaveRequestDto.getProductName() == "" || - voucherSaveRequestDto.getBarcode() == "" || - voucherSaveRequestDto.getExpiresAt() == "") { - throw new GptResponseException("GPT 응답이 올바르지 않습니다.", StatusEnum.NOT_FOUND); - } - return Mono.just(voucherSaveRequestDto); + voucherSaveRequestDto = createVoucherSaveRequestDto(response); } catch (JsonProcessingException e) { - e.printStackTrace(); - return Mono.error(new GptResponseException("GPT 응답이 올바르지 않습니다.", StatusEnum.NOT_FOUND)); + return Mono.error(new RuntimeException(e)); + } + if (voucherSaveRequestDto.getBrandName() == "" || voucherSaveRequestDto.getProductName() == "" || voucherSaveRequestDto.getBarcode() == "" + || voucherSaveRequestDto.getExpiresAt() == "") { + return Mono.error(new GptResponseException()); } + return Mono.just(voucherSaveRequestDto); }); } @@ -146,9 +138,7 @@ private VoucherSaveRequestDto createVoucherSaveRequestDto(GptResponseDto gptResp private String createQuery(VoucherSaveRequestDto voucherSaveRequestDto) { String queryTemplate = queryTemplateReader.readQueryTemplate(); - return String.format(queryTemplate, - voucherSaveRequestDto.getBrandName(), - voucherSaveRequestDto.getProductName()); + return String.format(queryTemplate, voucherSaveRequestDto.getBrandName(), voucherSaveRequestDto.getProductName()); } }