Skip to content

Commit

Permalink
fix: collect sentry errors
Browse files Browse the repository at this point in the history
  • Loading branch information
inh2613 committed Nov 7, 2023
1 parent 1916661 commit 9f98549
Showing 1 changed file with 21 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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
Expand All @@ -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<VoucherSaveRequestDto> 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);
});
}

Expand Down Expand Up @@ -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());
}

}

0 comments on commit 9f98549

Please sign in to comment.