catch process exceptions

This commit is contained in:
Denis Duliçi 2020-08-12 02:33:40 +03:00
parent 2d4d429758
commit d415cd6ae8
2 changed files with 28 additions and 9 deletions

View File

@ -61,7 +61,7 @@ class UpdateAll extends Command
$command = "update {$alias} {$company_id}";
if (true !== $result = Console::run($command, true)) {
if (true !== $result = Console::run($command)) {
$message = !empty($result) ? $result : trans('modules.errors.finish', ['module' => $alias]);
$this->error($message);

View File

@ -3,29 +3,48 @@
namespace App\Utilities;
use Illuminate\Console\Application;
use Illuminate\Support\Str;
use Symfony\Component\Process\Exception\InvalidArgumentException;
use Symfony\Component\Process\Exception\LogicException;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Process;
class Console
{
public static function run($string, $all_output = false, $timeout = 0)
public static function run($string, $timeout = 0)
{
$command = Application::formatCommandString($string);
logger('Console command:: ' . $command);
try {
$process = Process::fromShellCommandline($command, base_path());
$process->setTimeout($timeout);
$process->run();
$process->mustRun();
if ($process->isSuccessful()) {
$output = $process->getOutput();
if (static::isValidOutput($output)) {
return true;
}
$output = $all_output ? $process->getOutput() : $process->getErrorOutput();
} catch (InvalidArgumentException | LogicException | ProcessFailedException | RuntimeException $e) {
$output = $e->getMessage();
}
logger('Console output:: ' . $output);
return $output;
}
public static function isValidOutput($output)
{
$errors = [
'Content-Type: application/json',
'CSRF token mismatch',
];
return !Str::contains($output, $errors);
}
}