Reputation: 2783
I have a defined listener watching the onKernelException
in which errors could occur.
class SpecificExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof SpecificExceptionToBeProcessed) {
// ...
if ($somethingWentWrong) {
// here, redirect to the default/overriden Symfony error page
}
// ...
}
}
}
How do you redirect to the standard/customized error page in case of an error?
Upvotes: 1
Views: 1033
Reputation: 2783
For those who might be interested, here's how it can be done:
use Symfony\Bundle\TwigBundle\TwigEngine;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
// ...
class SpecificExceptionListener
{
protected $templating;
public function __construct(TwigEngine $templating)
{
$this->templating = $templating;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof SpecificExceptionToBeProcessed) {
// ...
if ($somethingWentWrong) {
// build response to display Symfony default error page
// replace by your own template if needed
$response = new Response();
$response->setContent(
$this->templating->render('TwigBundle:Exception:error.html.twig')
);
$event->setResponse($response);
return;
}
// ...
}
}
}
Upvotes: 1