I have a problem in a Symfony 4.4 application where there are a dozen listeners listening to a single event that extends Symfony\Contracts\EventDispatcher\Event
. The problem is that if one of the listeners throws an exception, then none of the listeners that come after it are executed.
I was looking for a way to catch exceptions from listeners so that this doesn't occur. I tried extending Symfony\Component\EventDispatcher\EventDispatcher
and overriding the callListeners()
method to include a try
/catch
so that I could log the exception but continue the execution. But I don't know how to tell Symfony to use my EventDispatcher
instead of its own.
I don't know if that's even a recommendable way of solving this problem. Any idea of how I could get this to work, or if there are any other alternatives?
If an exception is thrown, it should either be handled or the execution stopped. That's what exceptions are for.
And the Event Dispatcher component is not meant to be used asynchronously, with fully decoupled listeners. It's executed synchronously by design, had no transport support, and all listeners run on the same request/execution thread.
If one throws an Exception and it's not handled, execution should stop and never reach the next listener.
Even if you were using a fully async solution, like Symfony Messenger with a transport, throwing an exception and not handling it would prevent the execution of any other listeners to the same event.
I think you are using the wrong tools for the job.
Your listeners should not be throwing exceptions if you do not want to stop execution. Handle the exceptions in the listener, and use the opportunity to log, etc.