1414import com .back .global .ai .dto .AiRequest ;
1515import com .back .global .ai .dto .result .BaseScenarioResult ;
1616import com .back .global .ai .dto .result .DecisionScenarioResult ;
17+ import com .back .global .ai .exception .AiParsingException ;
1718import com .back .global .ai .exception .AiServiceException ;
1819import com .back .global .ai .prompt .BaseScenarioPrompt ;
1920import com .back .global .ai .prompt .DecisionScenarioPrompt ;
@@ -71,13 +72,17 @@ public CompletableFuture<BaseScenarioResult> generateBaseScenario(BaseLine baseL
7172 } catch (Exception e ) {
7273 log .error ("Failed to parse BaseScenario AI response for BaseLine ID: {}, error: {}" ,
7374 baseLine .getId (), e .getMessage (), e );
74- throw new AiServiceException (com .back .global .exception .ErrorCode .AI_RESPONSE_PARSING_ERROR ,
75- "Failed to parse BaseScenario response: " + e .getMessage ());
75+ throw new AiParsingException ("Failed to parse BaseScenario response: " + e .getMessage ());
7676 }
7777 })
7878 .exceptionally (e -> {
7979 log .error ("AI generation failed for BaseLine ID: {}, error: {}" ,
8080 baseLine .getId (), e .getMessage (), e );
81+ // AiParsingException은 그대로 전파, 나머지만 AiServiceException으로 감쌈
82+ Throwable cause = e .getCause () != null ? e .getCause () : e ;
83+ if (cause instanceof AiParsingException ) {
84+ throw (AiParsingException ) cause ;
85+ }
8186 throw new AiServiceException (com .back .global .exception .ErrorCode .AI_GENERATION_FAILED ,
8287 "Failed to generate base scenario: " + e .getMessage ());
8388 });
@@ -120,13 +125,17 @@ public CompletableFuture<DecisionScenarioResult> generateDecisionScenario(Decisi
120125 } catch (Exception e ) {
121126 log .error ("Failed to parse DecisionScenario AI response for DecisionLine ID: {}, error: {}" ,
122127 decisionLine .getId (), e .getMessage (), e );
123- throw new AiServiceException (com .back .global .exception .ErrorCode .AI_RESPONSE_PARSING_ERROR ,
124- "Failed to parse DecisionScenario response: " + e .getMessage ());
128+ throw new AiParsingException ("Failed to parse DecisionScenario response: " + e .getMessage ());
125129 }
126130 })
127131 .exceptionally (e -> {
128132 log .error ("AI generation failed for DecisionLine ID: {}, error: {}" ,
129133 decisionLine .getId (), e .getMessage (), e );
134+ // AiParsingException은 그대로 전파, 나머지만 AiServiceException으로 감쌈
135+ Throwable cause = e .getCause () != null ? e .getCause () : e ;
136+ if (cause instanceof AiParsingException ) {
137+ throw (AiParsingException ) cause ;
138+ }
130139 throw new AiServiceException (com .back .global .exception .ErrorCode .AI_GENERATION_FAILED ,
131140 "Failed to generate decision scenario: " + e .getMessage ());
132141 });
@@ -196,13 +205,17 @@ public CompletableFuture<String> generateSituation(List<DecisionNode> previousNo
196205 } catch (Exception e ) {
197206 log .error ("Failed to parse situation AI response, error: {}" ,
198207 e .getMessage (), e );
199- throw new AiServiceException (com .back .global .exception .ErrorCode .AI_RESPONSE_PARSING_ERROR ,
200- "Failed to parse situation response: " + e .getMessage ());
208+ throw new AiParsingException ("Failed to parse situation response: " + e .getMessage ());
201209 }
202210 })
203211 .exceptionally (e -> {
204212 log .error ("AI generation failed for situation, error: {}" ,
205213 e .getMessage (), e );
214+ // AiParsingException은 그대로 전파, 나머지만 AiServiceException으로 감쌈
215+ Throwable cause = e .getCause () != null ? e .getCause () : e ;
216+ if (cause instanceof AiParsingException ) {
217+ throw (AiParsingException ) cause ;
218+ }
206219 throw new AiServiceException (com .back .global .exception .ErrorCode .AI_GENERATION_FAILED ,
207220 "Failed to generate situation: " + e .getMessage ());
208221 });
@@ -224,9 +237,7 @@ public CompletableFuture<String> generateImage(String prompt) {
224237
225238 if (prompt == null || prompt .trim ().isEmpty ()) {
226239 log .warn ("Image prompt is empty. Returning placeholder." );
227- return CompletableFuture .failedFuture (
228- new AiServiceException (com .back .global .exception .ErrorCode .AI_INVALID_REQUEST ,
229- "Image prompt cannot be null or empty" ));
240+ return CompletableFuture .completedFuture ("placeholder-image-url" );
230241 }
231242
232243 log .info ("Generating image with prompt: {} (Storage: {})" , prompt , storageService .getStorageType ());
@@ -247,15 +258,12 @@ public CompletableFuture<String> generateImage(String prompt) {
247258 return storageService .uploadBase64Image (base64Data );
248259 })
249260 .exceptionally (e -> {
250- log .error ("Failed to generate or upload image: {}" , e .getMessage (), e );
251- throw new AiServiceException (com .back .global .exception .ErrorCode .AI_GENERATION_FAILED ,
252- "Failed to generate or upload image: " + e .getMessage ());
261+ log .warn ("Failed to generate or upload image, returning placeholder: {}" , e .getMessage ());
262+ return "placeholder-image-url" ;
253263 });
254264 } catch (Exception e ) {
255- log .error ("Error in generateImage, error: {}" , e .getMessage (), e );
256- return CompletableFuture .failedFuture (
257- new AiServiceException (com .back .global .exception .ErrorCode .AI_GENERATION_FAILED ,
258- "Unexpected error in image generation: " + e .getMessage ()));
265+ log .warn ("Error in generateImage, returning placeholder: {}" , e .getMessage ());
266+ return CompletableFuture .completedFuture ("placeholder-image-url" );
259267 }
260268 }
261269}
0 commit comments