@@ -376,8 +376,9 @@ public function createRuntime(
376376 * Execute any commands if they were provided
377377 */
378378 if ($ command !== '' && $ command !== '0 ' ) {
379+ $ stdout = '' ;
380+
379381 try {
380- $ stdout = '' ;
381382 $ status = $ this ->orchestration ->execute (
382383 name: $ runtimeName ,
383384 command: $ this ->getBuildCommands ($ command , $ version ),
@@ -386,7 +387,12 @@ public function createRuntime(
386387 );
387388
388389 if (!$ status ) {
389- throw new ExecutorException (ExecutorException::RUNTIME_FAILED , 'Failed to create runtime: ' . $ stdout );
390+ $ message = \trim ($ stdout );
391+
392+ throw new ExecutorException (
393+ ExecutorException::BUILD_FAILED ,
394+ $ message === '' ? 'Build command failed. ' : $ message
395+ );
390396 }
391397
392398 if ($ version === 'v2 ' ) {
@@ -398,6 +404,16 @@ public function createRuntime(
398404 } elseif (!$ cacheEnabled ) {
399405 $ output = Logs::get ($ runtimeName );
400406 }
407+ } catch (ExecutorException $ err ) {
408+ throw $ err ;
409+ } catch (OrchestrationException $ err ) {
410+ $ message = \trim ($ stdout );
411+
412+ throw new ExecutorException (
413+ ExecutorException::BUILD_FAILED ,
414+ $ message === '' ? 'Build command failed. ' : $ message ,
415+ previous: $ err
416+ );
401417 } catch (Throwable $ err ) {
402418 throw new ExecutorException (ExecutorException::RUNTIME_FAILED , $ err ->getMessage (), null , $ err );
403419 }
@@ -511,6 +527,26 @@ public function createRuntime(
511527 $ message .= $ chunk ['content ' ];
512528 }
513529
530+ if ($ throwable instanceof ExecutorException) {
531+ $ throwableMessage = \trim ($ throwable ->getMessage ());
532+
533+ if ($ message !== '' && \str_starts_with ($ throwableMessage , $ message )) {
534+ $ throwableMessage = \trim (\substr ($ throwableMessage , \strlen ($ message )));
535+ }
536+
537+ if ($ throwableMessage !== '' && ! \str_contains ($ message , $ throwableMessage )) {
538+ $ remaining = MAX_BUILD_LOG_SIZE - \strlen ($ message );
539+ $ remaining -= $ message === '' ? 0 : 1 ;
540+
541+ if ($ remaining > 0 ) {
542+ $ throwableMessage = \mb_strcut ($ throwableMessage , 0 , $ remaining );
543+ $ message .= $ message === '' ? $ throwableMessage : "\n" . $ throwableMessage ;
544+ }
545+ }
546+
547+ throw new ExecutorException ($ throwable ->getType (), $ message , $ throwable ->getCode (), $ throwable );
548+ }
549+
514550 throw new \Exception ($ message , $ throwable ->getCode () ?: 500 , $ throwable );
515551 }
516552
@@ -545,14 +581,14 @@ private function getBuildCommands(string $command, string $version): array
545581 return [
546582 'sh ' ,
547583 '-c ' ,
548- 'touch /var/tmp/logs.txt && ( ' . $ command . ') >> /var/tmp/logs.txt 2>&1 && cat /var/tmp/logs.txt '
584+ 'touch /var/tmp/logs.txt && ( ' . $ command . ') >> /var/tmp/logs.txt 2>&1; status=$?; cat /var/tmp/logs.txt; if [ $status -ne 0 ]; then echo "Build command exited with code $status."; fi; exit $status '
549585 ];
550586 }
551587
552588 return [
553589 'bash ' ,
554590 '-c ' ,
555- 'mkdir -p /tmp/logging && touch /tmp/logging/timings.txt && touch /tmp/logging/logs.txt && script --log-out /tmp/logging/logs.txt --flush --log-timing /tmp/logging/timings.txt --return --quiet --command " ' . \str_replace ('" ' , '\" ' , $ command ) . '" '
591+ 'mkdir -p /tmp/logging && touch /tmp/logging/timings.txt && touch /tmp/logging/logs.txt && script --log-out /tmp/logging/logs.txt --flush --log-timing /tmp/logging/timings.txt --return --quiet --command " ' . \str_replace ('" ' , '\" ' , $ command ) . '"; status=$?; if [ $status -ne 0 ]; then echo "Build command exited with code $status."; fi; exit $status '
556592 ];
557593 }
558594
0 commit comments