diff --git a/src/main/java/org/swmaestro/repl/gifthub/exception/GptResponseException.java b/src/main/java/org/swmaestro/repl/gifthub/exception/GptResponseException.java index 14dd8d90..3f23182f 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/exception/GptResponseException.java +++ b/src/main/java/org/swmaestro/repl/gifthub/exception/GptResponseException.java @@ -2,19 +2,8 @@ import org.swmaestro.repl.gifthub.util.StatusEnum; -import io.sentry.Sentry; - -public class GptResponseException extends RuntimeException { - private StatusEnum status; - - public GptResponseException(String message, StatusEnum status) { - super(message); - this.status = status; - captureExceptionWithSentry(this); - } - - private void captureExceptionWithSentry(Throwable throwable) { - Sentry.captureException(throwable); - +public class GptResponseException extends BusinessException { + public GptResponseException() { + super("GPT 응답이 올바르지 않습니다.", StatusEnum.NOT_FOUND); } } \ No newline at end of file diff --git a/src/main/java/org/swmaestro/repl/gifthub/exception/GptTimeoutException.java b/src/main/java/org/swmaestro/repl/gifthub/exception/GptTimeoutException.java new file mode 100644 index 00000000..4ca2632a --- /dev/null +++ b/src/main/java/org/swmaestro/repl/gifthub/exception/GptTimeoutException.java @@ -0,0 +1,9 @@ +package org.swmaestro.repl.gifthub.exception; + +import org.swmaestro.repl.gifthub.util.StatusEnum; + +public class GptTimeoutException extends BusinessException { + public GptTimeoutException() { + super("GPT 요청이 시간초과되었습니다.", StatusEnum.NOT_FOUND); + } +} diff --git a/src/main/java/org/swmaestro/repl/gifthub/exception/TimeoutException.java b/src/main/java/org/swmaestro/repl/gifthub/exception/TimeoutException.java deleted file mode 100644 index b76d0230..00000000 --- a/src/main/java/org/swmaestro/repl/gifthub/exception/TimeoutException.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.swmaestro.repl.gifthub.exception; - -import org.swmaestro.repl.gifthub.util.StatusEnum; - -import io.sentry.Sentry; - -public class TimeoutException extends RuntimeException { - private StatusEnum status; - - public TimeoutException(String message, StatusEnum status) { - super(message); - this.status = status; - captureExceptionWithSentry(this); - } - - private void captureExceptionWithSentry(Throwable throwable) { - Sentry.captureException(throwable); - - } -} diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/GptService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/GptService.java index 21b12d4b..066ae0f0 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/GptService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/GptService.java @@ -35,7 +35,7 @@ public GptService(WebClient.Builder webClientBuilder, @Value("/gpt/question.txt" this.prompt = loadQuestionFromFile(promptPath); } - public Mono getGptResponse(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto) throws IOException { + public Mono getGptResponse(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto) { String question = prompt; String content = voucherAutoSaveRequestDto.concatenateTexts(); 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()); } }