You should create a listener that listens on kernel.exception
event. In onKernelException
method of that listener you can check for your exception e.g
On exception listener class
//namespace declarations
class YourExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof YourException) {
//create response, set status code etc.
$event->setResponse($response); //event will stop propagating here. Will not call other listeners.
}
}
}
The service declaration would be
//services.yml
kernel.listener.yourlisener:
class: FQCN\Of\YourExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }