Reputation: 9552
Using Syfmony2, I have created custom error templates, in my case it is
/app/Resources/TwigBundle/views/Exception/error.html.twig
and looks somehow like this
<html>
</body>
<h1>ERROR {{ status_code }}</h1>
<p>{{ status_text }}</p>
</body>
</html>
When I now throw an error with a message in it:
throw new Exception('There is something wrong in the state of Denkmark.');
I would expect that the message is shown on the rendered error template. Instead, it only shows a standard message:
Internal Server Error
However, when I run in dev mode it shows the correct message (but on the Symfony2 standard error template). Why is the message in prod mode hidden? Who am I writing the messages for? For the dev log?
(How) can I force to show the message on my template in prod mode as well?
Upvotes: 1
Views: 1050
Reputation: 879
You will nedd to make a custom template, EventListener and register the EventListener:
// src/Acme/DemoBundle/EventListener/AcmeExceptionListener.php
namespace Acme\DemoBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
class AcmeExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
// You get the exception object from the received event
$exception = $event->getException();
$message = sprintf(
'My Error says: %s with code: %s',
$exception->getMessage(),
$exception->getCode()
);
// Customize your response object to display the exception details
$response = new Response();
$response->setContent($message);
// HttpExceptionInterface is a special type of exception that
// holds status code and header details
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
}
// Send the modified response object to the event
$event->setResponse($response);
}
}
and you will need to register the listenter:
# app/config/config.yml
services:
kernel.listener.your_listener_name:
class: Acme\DemoBundle\EventListener\AcmeExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method:
onKernelException }
source: http://symfony.com/doc/current/cookbook/service_container/event_listener.html
Upvotes: 1
Reputation: 29912
This behaviour is correct as Exception
may contains some "internal" information and in production enviroment those infos should not be displayed.
What you can do is costumize the 404 page or use your own logic to display something when an exception occurs but you can't rely on symfony2 standard logic as it's not compatible with your
For costumize 404 page, you should ovveride default template by placing your own error page here app/Resources/TwigBundle/views/Exception/error404.html.twig
For your own logic: simply use an event listener/subscriber that will render a page upon some exceptions
Upvotes: 2