Skip to content

Commit

Permalink
catch process exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Aug 11, 2020
1 parent 2d4d429 commit d415cd6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/UpdateAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function runUpdate($alias)

$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);
Expand Down
35 changes: 27 additions & 8 deletions app/Utilities/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

$process = Process::fromShellCommandline($command, base_path());
$process->setTimeout($timeout);
try {
$process = Process::fromShellCommandline($command, base_path());
$process->setTimeout($timeout);

$process->run();
$process->mustRun();

if ($process->isSuccessful()) {
return true;
}
$output = $process->getOutput();

$output = $all_output ? $process->getOutput() : $process->getErrorOutput();
if (static::isValidOutput($output)) {
return true;
}
} 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);
}
}

0 comments on commit d415cd6

Please sign in to comment.