Skip to content

Commit

Permalink
Send the right HTTP status code for each exception
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Nov 3, 2015
1 parent 908ec13 commit 3d798f7
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions EventListener/ExceptionListener.php
Expand Up @@ -13,6 +13,7 @@

use JavierEguiluz\Bundle\EasyAdminBundle\Exception\BaseException;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;

/**
* This listener allows to display customized error pages in the production
Expand All @@ -25,10 +26,10 @@ class ExceptionListener
private $templating;
private $debug;
private $exceptionTemplates = array(
'ForbiddenActionException' => '@EasyAdmin/error/forbidden_action.html.twig',
'NoEntitiesConfigurationException' => '@EasyAdmin/error/no_entities.html.twig',
'UndefinedEntityException' => '@EasyAdmin/error/undefined_entity.html.twig',
'EntityNotFoundException' => '@EasyAdmin/error/entity_not_found.html.twig',
'ForbiddenActionException' => array('template' => '@EasyAdmin/error/forbidden_action.html.twig', 'status' => 403),
'NoEntitiesConfigurationException' => array('template' => '@EasyAdmin/error/no_entities.html.twig', 'status' => 500),
'UndefinedEntityException' => array('template' => '@EasyAdmin/error/undefined_entity.html.twig', 'status' => 500),
'EntityNotFoundException' => array('template' => '@EasyAdmin/error/entity_not_found.html.twig', 'status' => 404),
);

public function __construct($templating, $debug)
Expand All @@ -52,9 +53,11 @@ public function onKernelException(GetResponseForExceptionEvent $event)
return;
}

$templatePath = $this->exceptionTemplates[$exceptionClassName];
$templatePath = $this->exceptionTemplates[$exceptionClassName]['template'];
$reponseStatusCode = $this->exceptionTemplates[$exceptionClassName]['status'];
$parameters = array_merge($exception->getParameters(), array('message' => $exception->getMessage()));
$response = $this->templating->renderResponse($templatePath, $parameters);

$response = $this->templating->renderResponse($templatePath, $parameters, new Response('', $reponseStatusCode));

$event->setResponse($response);
}
Expand Down

0 comments on commit 3d798f7

Please sign in to comment.