Skip to content

swap terminate and exception event descriptions #6638

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

Merged
merged 1 commit into from
Jun 12, 2016
Merged
Changes from all commits
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
60 changes: 30 additions & 30 deletions components/console/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@ C/C++ standard.::
}
});

The ``ConsoleEvents::EXCEPTION`` Event
--------------------------------------

**Typical Purposes**: Handle exceptions thrown during the execution of a
command.

Whenever an exception is thrown by a command, the ``ConsoleEvents::EXCEPTION``
event is dispatched. A listener can wrap or change the exception or do
anything useful before the exception is thrown by the application.

Listeners receive a
:class:`Symfony\\Component\\Console\\Event\\ConsoleExceptionEvent` event::

use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\ConsoleEvents;

$dispatcher->addListener(ConsoleEvents::EXCEPTION, function (ConsoleExceptionEvent $event) {
$output = $event->getOutput();

$command = $event->getCommand();

$output->writeln(sprintf('Oops, exception thrown while running command <info>%s</info>', $command->getName()));

// get the current exit code (the exception code or the exit code set by a ConsoleEvents::TERMINATE event)
$exitCode = $event->getExitCode();

// change the exception to another one
$event->setException(new \LogicException('Caught exception', $exitCode, $event->getException()));
});

The ``ConsoleEvents::TERMINATE`` Event
--------------------------------------

Expand Down Expand Up @@ -127,34 +157,4 @@ Listeners receive a
It is then dispatched just after the ``ConsoleEvents::EXCEPTION`` event.
The exit code received in this case is the exception code.

The ``ConsoleEvents::EXCEPTION`` Event
--------------------------------------

**Typical Purposes**: Handle exceptions thrown during the execution of a
command.

Whenever an exception is thrown by a command, the ``ConsoleEvents::EXCEPTION``
event is dispatched. A listener can wrap or change the exception or do
anything useful before the exception is thrown by the application.

Listeners receive a
:class:`Symfony\\Component\\Console\\Event\\ConsoleExceptionEvent` event::

use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\ConsoleEvents;

$dispatcher->addListener(ConsoleEvents::EXCEPTION, function (ConsoleExceptionEvent $event) {
$output = $event->getOutput();

$command = $event->getCommand();

$output->writeln(sprintf('Oops, exception thrown while running command <info>%s</info>', $command->getName()));

// get the current exit code (the exception code or the exit code set by a ConsoleEvents::TERMINATE event)
$exitCode = $event->getExitCode();

// change the exception to another one
$event->setException(new \LogicException('Caught exception', $exitCode, $event->getException()));
});

.. _`reserved exit codes`: http://www.tldp.org/LDP/abs/html/exitcodes.html