55import com .tave .alarmissue .ai .dto .response .ThemaResponse ;
66import lombok .RequiredArgsConstructor ;
77import lombok .extern .slf4j .Slf4j ;
8+ import org .springframework .http .HttpStatusCode ;
89import org .springframework .core .ParameterizedTypeReference ;
910import org .springframework .http .HttpHeaders ;
1011import org .springframework .http .MediaType ;
@@ -30,10 +31,22 @@ public Mono<ThemaResponse> analyzeThema(String text) {
3031
3132 return webClientForThema .post ()
3233 .uri ("/predict" )
33- .contentType (MediaType .APPLICATION_JSON ) // 요청 바디 타입
34+ .contentType (MediaType .APPLICATION_JSON )
3435 .bodyValue (Map .of ("text" , text ))
3536 .retrieve ()
36- .bodyToMono (ThemaResponse .class );
37+ .onStatus (HttpStatusCode ::isError , response ->
38+ response .bodyToMono (String .class )
39+ .defaultIfEmpty ("Unknown error" )
40+ .flatMap (body -> {
41+ log .error ("Thema API error: {}" , body );
42+ return Mono .error (new RuntimeException ("Thema API error: " + body ));
43+ })
44+ )
45+ .bodyToMono (ThemaResponse .class )
46+ .onErrorResume (e -> {
47+ log .error ("Thema 요청 실패" , e );
48+ return Mono .just (new ThemaResponse ("분석 실패" )); // 또는 null, Optional, 에러 DTO 등
49+ });
3750 }
3851
3952 public Mono <SummaryResponse > analyzeSummary (String text ) {
@@ -42,7 +55,18 @@ public Mono<SummaryResponse> analyzeSummary(String text) {
4255 .header (HttpHeaders .ACCEPT , MediaType .APPLICATION_JSON_VALUE )
4356 .bodyValue (Map .of ("text" , text ))
4457 .retrieve ()
45- .bodyToMono (SummaryResponse .class );
58+ .onStatus (HttpStatusCode ::isError , response ->
59+ response .bodyToMono (String .class )
60+ .flatMap (body -> {
61+ log .error ("Summary API error: {}" , body );
62+ return Mono .error (new RuntimeException ("Summary API error: " + body ));
63+ })
64+ )
65+ .bodyToMono (SummaryResponse .class )
66+ .onErrorResume (e -> {
67+ log .error ("Summary 요청 실패" , e );
68+ return Mono .just (new SummaryResponse ("요약 실패" )); // 필요에 따라 수정
69+ });
4670 }
4771
4872 public Mono <SentimentResponse > analyzeSentiment (List <String > titles ) {
@@ -57,7 +81,12 @@ public Mono<SentimentResponse> analyzeSentiment(List<String> titles) {
5781 return response .bodyToMono (new ParameterizedTypeReference <List <Map <String , Double >>>() {
5882 });
5983 } else {
60- return response .createException ().flatMap (Mono ::error );
84+ return response .bodyToMono (String .class )
85+ .defaultIfEmpty ("Unknown error" )
86+ .flatMap (body -> {
87+ log .error ("Sentiment API error body: {}" , body );
88+ return Mono .error (new RuntimeException ("Sentiment API error: " + body ));
89+ });
6190 }
6291 })
6392 .map (results -> {
@@ -68,6 +97,10 @@ public Mono<SentimentResponse> analyzeSentiment(List<String> titles) {
6897 .floatValue ();
6998
7099 return new SentimentResponse (roundedScore );
100+ })
101+ .onErrorResume (e -> {
102+ log .error ("Sentiment 요청 실패" , e );
103+ return Mono .just (new SentimentResponse (0.0f )); // 또는 실패 응답 정의
71104 });
72105 }
73106}
0 commit comments