ProcessFailedException.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Process\Exception;
  11. use Symfony\Component\Process\Process;
  12. /**
  13. * Exception for failed processes.
  14. *
  15. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  16. */
  17. class ProcessFailedException extends RuntimeException
  18. {
  19. private Process $process;
  20. public function __construct(Process $process)
  21. {
  22. if ($process->isSuccessful()) {
  23. throw new InvalidArgumentException('Expected a failed process, but the given process was successful.');
  24. }
  25. $error = sprintf('The command "%s" failed.'."\n\nExit Code: %s(%s)\n\nWorking directory: %s",
  26. $process->getCommandLine(),
  27. $process->getExitCode(),
  28. $process->getExitCodeText(),
  29. $process->getWorkingDirectory()
  30. );
  31. if (!$process->isOutputDisabled()) {
  32. $error .= sprintf("\n\nOutput:\n================\n%s\n\nError Output:\n================\n%s",
  33. $process->getOutput(),
  34. $process->getErrorOutput()
  35. );
  36. }
  37. parent::__construct($error);
  38. $this->process = $process;
  39. }
  40. public function getProcess(): Process
  41. {
  42. return $this->process;
  43. }
  44. }