I have a controller that processes API requests. Now I need to handle any exceptions for this controller separately, that is, convert to json / xml format, redefine the message or add any details. Exception handling all over the world is not suitable in my case.
Today I have a KernelEvent subscriber that handles kernel.exception and detects the current controller ( Gist ):
public function processApiException(GetResponseForExceptionEvent $event)
{
$controllerName = $this->container->get('controller_name_converter');
$resolver = new ControllerResolver($this->container, controllerName);
$controller = $resolver->getController($event->getRequest());
if ($contoller && $controller[0] instanceof JsonApiControllerInterface) {
$this->handleApiExceptionEvent($event);
}
}
So the questions are:
- Is this a good solution to handle specific controller exceptions this way?
- Is there any other method to get the current controller (from GetResponseForExceptionEvent) other than using ControllerResolver?
Ostin