8000 added catching of php7 fatal exceptions in application #20110 by bozerkins · Pull Request #20111 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

added catching of php7 fatal exceptions in application #20110 #20111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added catching throwable exceptions when running console application
- implemented compatibilit with PHP7 fatal errors
- using FatalThrowableError for BC with PHP5
  • Loading branch information
Bogdans Ozerkins committed Oct 1, 2016
commit 999d7bc62cfbeb85fbae9e679f4c17b7007c8cc4
63 changes: 27 additions & 36 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function setDispatcher(EventDispatcherInterface $dispatcher)
*
* @return int 0 if everything went fine, or an error code
*
* @throws \Exception When doRun returns Exception
* @throws \Exception When doRun returns Exception or Throwable
*/
public function run(InputInterface $input = null, OutputInterface $output = null)
{
Expand All @@ -120,53 +120,44 @@ public function run(InputInterface $input = null, OutputInterface $output = null

$this->configureIO($input, $output);

$exception = null;
$exitCode = null;
try {
$exitCode = $this->doRun($input, $output);
} catch (\Exception $e) {
$exitCode = $this->handleCommandRunException($e, $output);
$exception = $e;
} catch (\Throwable $e) {
$exitCode = $this->handleCommandRunException($e, $output);
$exception = new FatalThrowableError($e);;
}

if ($this->autoExit) {
if ($exitCode > 255) {
$exitCode = 255;
if ($exception) {
if (!$this->catchExceptions) {
throw $exception;
Copy link
Member
@nicolas-grekas nicolas-grekas Oct 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$e should be thrown here, not $exception (no wrapper)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean FatalThrowableError should not be here at all?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, when rethrowing, the original exception should be thrown

}

exit($exitCode);
}

return $exitCode;
}

/**
* Handler command run exception and provides with the exit code in return
*
* @param \Exception|\Throwable $e
* @param OutputInterface $output
* @return int|mixed
* @throws \Throwable
*/
private function handleCommandRunException($e, OutputInterface $output)
{
if (!$this->catchExceptions) {
throw $e;
}
if ($output instanceof ConsoleOutputInterface) {
$this->renderException($exception, $output->getErrorOutput());
} else {
$this->renderException($exception, $output);
}

if ($output instanceof ConsoleOutputInterface) {
$this->renderException($e, $output->getErrorOutput());
} else {
$this->renderException($e, $output);
$exitCode = $exception->getCode();
if (is_numeric($exitCode)) {
$exitCode = (int) $exitCode;
if (0 === $exitCode) {
$exitCode = 1;
}
} else {
$exitCode = 1;
}
}

$exitCode = $e->getCode();
if (is_numeric($exitCode)) {
$exitCode = (int) $exitCode;
if (0 === $exitCode) {
$exitCode = 1;
if ($this->autoExit) {
if ($exitCode > 255) {
$exitCode = 255;
}
} else {
$exitCode = 1;

exit($exitCode);
}

return $exitCode;
Expand Down
0