1
votes

Following the docs http://symfony.com/doc/current/cookbook/controller/error_pages.html I have been able to add my own template for errors;

app
 |- Resources
 |  | - TwigBundle
 |  |  |- view
 |  |  |  |- Exception
 |  |  |  |  |- exception.html.twig
 |  |  |  |  |- error.html.twig
 |  |  |  |  |- error400.html.twig
 |  |  |  |  |- error404.html.twig
 |  |  |  |  |- error500.html.twig
 |  |  |  |- layout.html.twig
 |- config

This works perfectly, but how do I keep the stack traces and the detailed errors for my dev environment?

In production I wish to use my own templates, in the dev environment I wish to use Symfony2's own templates.

2
I have just been doing the same last night and as far as I could see on Symfony2 (v2.5) still threw "green" alien page with complete stacktrace, while I was in dev mode. However, if I move to prod, my custom error page is shown instead.Jovan Perovic
That doesn't seem to be the case for me, hmmm. have you set up your templates the same as me within app?Jake N
Similarly, I only had error403.html.twig. Do you use Symfony 2.6 on something else? They have made some things easier with previewing error pages as of 2.6.Jovan Perovic
2.5, I have error and exception. Does 403 get overridden in dev mode for you?Jake N
If dev no, but in prod yes. At one point I wished that was the case as it really slowed down the testing of error403.html.twig layout (had to cache:clear after each change...)Jovan Perovic

2 Answers

3
votes
app
 |- Resources
 |  |- TwigBundle
 |  |  |- view
 |  |  |  |- Exception
 |  |  |  |  |- exception.html.twig
 |  |  |  |  |- error.html.twig
 |  |  |  |  |- error400.html.twig
 |  |  |  |  |- error404.html.twig
 |  |  |  |  |- error500.html.twig
 |  |  |  |- layout.html.twig
 |  |- view
 |  |  |- my_custom_error_layout.html.twig
 |- config

my_custom_error_layout.html.twig

<!DOCTYPE>
<html>
<head>
</head>
<body>
html, block, whatnot go here
</body>
</html>

error400.html.twig

{% extends "::my_custom_error_layout.html.twig" %}

{% block body %}
details or fixed message go here...
{% endblock %}
1
votes

In our recent project we implement a custom ExceptionListener configured as above:

    <!-- Acme Exception Listener -->
    <service id="kernel.listener.customer_area_exception_listener" class="AcmeSecurityBundle\Listener\AcmeExceptionListener">
        <argument type="service" id="templating" />
        <argument>%acme.exceptions.debug%</argument>
        <tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
    </service>

With a parameter (from parameters.yml ) for differentiate the behaviour in the env.

The exception listener know how render every type of Exception, custom Exception also.

Hope this help