@@ -210,10 +210,59 @@ public function setDefaultsFromDataSourceConfig(array $config)
210210 public static function convertResponseToException ($ result )
211211 {
212212 if ($ result ['status ' ] === 'error ' ) {
213- if (str_starts_with ($ result ['message ' ], 'Command exceeded timeout of ' )) {
214- throw new ScriptTimeoutException ($ result ['message ' ]);
213+ $ rawMessage = $ result ['message ' ] ?? '' ;
214+ if (str_starts_with ((string ) $ rawMessage , 'Command exceeded timeout of ' )) {
215+ throw new ScriptTimeoutException ((string ) $ rawMessage );
216+ }
217+
218+ $ message = self ::extractScriptErrorMessage ($ result );
219+
220+ if (empty ($ message )) {
221+ $ message = $ rawMessage ?: 'Script execution failed with unknown error ' ;
222+ }
223+
224+ throw new ScriptException ($ message );
225+ }
226+ }
227+
228+ /**
229+ * Extract a concise error message from the microservice response.
230+ */
231+ private static function extractScriptErrorMessage (array $ result ): string
232+ {
233+ $ candidates = [
234+ $ result ['output ' ]['error ' ] ?? null ,
235+ $ result ['output ' ]['exception ' ] ?? null ,
236+ $ result ['output ' ]['stderr ' ] ?? null ,
237+ $ result ['output ' ]['stdout ' ] ?? null ,
238+ $ result ['message ' ] ?? null ,
239+ ];
240+
241+ foreach ($ candidates as $ candidate ) {
242+ if (is_string ($ candidate ) || is_numeric ($ candidate )) {
243+ $ short = self ::shortenMessage ((string ) $ candidate );
244+ if (!empty ($ short )) {
245+ return $ short ;
246+ }
215247 }
216- throw new ScriptException ($ result ['message ' ]);
217248 }
249+
250+ return '' ;
251+ }
252+
253+ /**
254+ * Keep only the first line of the error and limit its length to avoid noisy traces.
255+ */
256+ private static function shortenMessage (string $ message ): string
257+ {
258+ $ firstLine = strtok ($ message , "\n" );
259+ $ firstLine = $ firstLine === false ? $ message : $ firstLine ;
260+ $ trimmed = trim ($ firstLine );
261+
262+ if (strlen ($ trimmed ) > 400 ) {
263+ return substr ($ trimmed , 0 , 400 ) . '… ' ;
264+ }
265+
266+ return $ trimmed ;
218267 }
219268}
0 commit comments