New in Symfony 3.3: Better handling of command exceptions

Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.

Contributed by
Wouter De Jong
in #18140.

The ConsoleEvents::EXCEPTION event is triggered as soon as an exception is thrown while running a console command. It's useful to change the exceptions or handle them in some way before the application throws them.

However, this event is only dispatched for exceptions during the execution of Command#execute(). All other exceptions (e.g. the ones thrown by listeners) are silently caught in the Application#doRunCommand() method. This means that there is no way to override exception handling in those cases.

In Symfony 3.3, we decided to improve the exception handling in commands with the introduction of a new ConsoleEvents::ERROR event and the deprecation of the ConsoleEvents::EXCEPTION event. A new ConsoleErrorEvent class has also been created and the ConsoleExceptionEvent class has been deprecated too.

In practice, the changes required in your application will be minimal. Just listen to the new ConsoleEvents::ERROR event and handle the exceptions as before, with the added possibility of handling more exceptions that may occur during the command execution.

Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

New in Symfony 3.3: Better handling of command exceptions symfony.com/blog/new-in-symfony-3-3-better-handling-of-command-exceptions

Tweet this

Comments

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.