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