In my Module.php I have attached a handler to catch exceptions. This works, but I'm not sure how I setup a response and view template.
My code below attempts to set the responce code to 409 and display the 409.phtml template. However it does not work, it just renders the original 500 error page.
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$eventManager->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'handleError'));
}
public function handleError(MvcEvent $event)
{
//get the exception
$exception = $event->getParam('exception');
if (is_a($exception, 'Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException'))
{
$response = $event->getResponse();
$response = $response ?: new Response();
$model = new ViewModel(array());
$model->setTemplate('error\409');
$event->getViewModel()->addChild($model);
$response->setStatusCode(409);
$event->setResponse($response);
return $response;
}
}