исключение — попробуйте .. поймать с помощью команды system () php на laravel 5.0

У меня есть код для командного приложения, созданный на laravel 5.0 с помощью команды try .. catch and use system. Когда есть исключение, мой улов не работает.

use Illuminate\Support\Facades\Log;
...
try {
system('.dump master | sqlite3 ' . $older . ' > ' . storage_path() . '/tmp/' . $tabla . '.sql' );
} catch ( \Exception $e) {
Log::alert('Problem import old table '. $tabla . $e->GetMessage());
}

Я вижу свою ошибку в оболочке, но не пишу в своем журнале Laravel. (Вход: предупреждение)

0

Решение

Вы должны получить доступ через STDERR и, возможно, STDOUT, использование proc_open, например.:

$desc = [
1 => ['pipe', 'w'], // STDOUT
2 => ['pipe', 'w'], // STDERR
];

$proc = proc_open('ls -l . something', $desc, $pipes);
if (is_resource($proc)) {

if ($out = stream_get_contents($pipes[1])) {
echo $out;
}
fclose($pipes[1]);if ($err = stream_get_contents($pipes[2])) {
fprintf(STDERR, "Error: %s\n", $err);
}
fclose($pipes[2]);

// You can also check the process exit status
// 0 means success, otherwise error.
$exit_status = proc_close($proc);
}

Конечно, нет необходимости в STDOUT труба, если команда перенаправляет его в файл.

И да, system() не будет бросать исключения. Очевидно, что вы можете реализовать свой собственный класс, который будет генерировать исключение в случае, если статус завершения процесса не равен нулю или что-то перехватывается в STDERR трубы:

class MyShellException extends \Exception {}

class MyShell {
public static function execute($command, &$out = null) {
if (func_num_args() > 1) {
$desc[1] = ['pipe', 'w'];
} else {
$desc[1] = ['file', '/dev/null'];
}

$desc[2] = ['pipe', 'w'];

$proc = proc_open($command, $desc, $pipes);
if (is_resource($proc)) {
if (isset($pipes[1])) {
$out = stream_get_contents($pipes[1]);
fclose($pipes[1]);
}

if ($err = stream_get_contents($pipes[2])) {
fclose($pipes[2]);
throw new MyShellException("Command $command failed: $err");
}

if ($exit_status = proc_close($proc)) {
throw new MyShellException("Command $command exited with non-zero status");
}
}
}
}try {
MyShell::execute('ls -l . something', $out);
echo "Output: $out\n";
} catch (MyShellException $e) {
if (!empty($out)) {
echo "Output: $out\n";
}
fprintf(STDERR, "MyShell error: " . $e->getMessage());
exit(1);
}
2

Другие решения

Используйте Throw Exception в php.
Вот ссылка ссылка использовать и пример примера:

<?php
/*
*
* opcode number: 108
*/
try {
$error = 'Always throw this error';
throw new Exception($error);

// Code following an exception is not executed.
echo 'Never executed';

} catch (Exception $e) {
echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo 'Hello World';
?>
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector