master

laravel/framework

Last updated at: 29/12/2023 09:25

ProcessFailedException.php

TLDR

This file defines the ProcessFailedException class, which extends the RuntimeException class. It is used to represent an exception when a process fails.

Classes

ProcessFailedException

The ProcessFailedException class extends the RuntimeException class. It represents an exception when a process fails. It has a public $result property which holds an instance of the ProcessResult class. The class has one constructor method that takes a ProcessResult object as an argument. The constructor sets the $result property and generates an error message based on the command, exit code, output, and error output of the process. The error message is then passed to the parent constructor of RuntimeException, along with the exit code of the process.

<?php

namespace Illuminate\Process\Exceptions;

use Illuminate\Contracts\Process\ProcessResult;
use RuntimeException;

class ProcessFailedException extends RuntimeException
{
    /**
     * The process result instance.
     *
     * @var \Illuminate\Contracts\Process\ProcessResult
     */
    public $result;

    /**
     * Create a new exception instance.
     *
     * @param  \Illuminate\Contracts\Process\ProcessResult  $result
     * @return void
     */
    public function __construct(ProcessResult $result)
    {
        $this->result = $result;

        $error = sprintf('The command "%s" failed.'."\n\nExit Code: %s",
            $result->command(),
            $result->exitCode(),
        );

        if (! empty($result->output())) {
            $error .= sprintf("\n\nOutput:\n================\n%s", $result->output());
        }

        if (! empty($result->errorOutput())) {
            $error .= sprintf("\n\nError Output:\n================\n%s", $result->errorOutput());
        }

        parent::__construct($error, $result->exitCode() ?? 1);
    }
}