Skip to content

Commit

Permalink
Collect Sentry errors (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
inh2613 authored Nov 7, 2023
2 parents 080040a + 9f98549 commit ace586a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public GptService(WebClient.Builder webClientBuilder, @Value("/gpt/question.txt"
this.prompt = loadQuestionFromFile(promptPath);
}

public Mono<GptResponseDto> getGptResponse(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto) throws IOException {
public Mono<GptResponseDto> getGptResponse(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto) {
String question = prompt;
String content = voucherAutoSaveRequestDto.concatenateTexts();

Expand Down
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 ace586a

Please sign in to comment.