Skip to content

Commit

Permalink
moved the Exception listener from FrameworkBundle to TwigBundle as it…
Browse files Browse the repository at this point in the history
… relies on Twig being enabled

This commit also fixes exception pages when Twig is not enabled as a templating engine.
Instead of just displaying the raw Twig template as before, we now fallback to the default
exception handler introduced some time ago.
  • Loading branch information
fabpot committed Jul 21, 2011
1 parent 08730b6 commit 3749ad4
Show file tree
Hide file tree
Showing 42 changed files with 82 additions and 37 deletions.
Expand Up @@ -49,7 +49,6 @@ public function getConfigTreeBuilder()
->scalarNode('charset')->end()
->scalarNode('trust_proxy_headers')->defaultFalse()->end()
->scalarNode('secret')->isRequired()->end()
->scalarNode('exception_controller')->defaultValue('Symfony\\Bundle\\FrameworkBundle\\Controller\\ExceptionController::showAction')->end()
->scalarNode('ide')->defaultNull()->end()
->booleanNode('test')->end()
->end()
Expand Down
Expand Up @@ -62,7 +62,6 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('kernel.charset', $config['charset']);
}
$container->setParameter('kernel.secret', $config['secret']);
$container->setParameter('exception_listener.controller', $config['exception_controller']);

$container->setParameter('kernel.trust_proxy_headers', $config['trust_proxy_headers']);

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Expand Up @@ -50,7 +50,7 @@ public function build(ContainerBuilder $container)

$container->addCompilerPass(new RoutingResolverPass());
$container->addCompilerPass(new ProfilerPass());
$container->addCompilerPass(new RegisterKernelListenersPass());
$container->addCompilerPass(new RegisterKernelListenersPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new TemplatingPass());
$container->addCompilerPass(new AddConstraintValidatorsPass());
$container->addCompilerPass(new AddValidatorInitializersPass());
Expand Down
8 changes: 0 additions & 8 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml
Expand Up @@ -10,7 +10,6 @@
<parameter key="controller_resolver.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver</parameter>
<parameter key="controller_name_converter.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser</parameter>
<parameter key="response_listener.class">Symfony\Component\HttpKernel\EventListener\ResponseListener</parameter>
<parameter key="exception_listener.class">Symfony\Component\HttpKernel\EventListener\ExceptionListener</parameter>
</parameters>

<services>
Expand Down Expand Up @@ -45,12 +44,5 @@
<tag name="kernel.event_listener" event="kernel.response" method="onKernelResponse" />
<argument>%kernel.charset%</argument>
</service>

<service id="exception_listener" class="%exception_listener.class%">
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-128" />
<tag name="monolog.logger" channel="request" />
<argument>%exception_listener.controller%</argument>
<argument type="service" id="logger" on-invalid="null" />
</service>
</services>
</container>

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Controller;
namespace Symfony\Bundle\TwigBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\DependencyInjection\ContainerAware;
Expand Down Expand Up @@ -75,21 +75,21 @@ protected function findTemplate($templating, $format, $code, $debug)

// when not in debug, try to find a template for the specific HTTP status code and format
if (!$debug) {
$template = new TemplateReference('FrameworkBundle', 'Exception', $name.$code, $format, 'twig');
$template = new TemplateReference('TwigBundle', 'Exception', $name.$code, $format, 'twig');
if ($templating->exists($template)) {
return $template;
}
}

// try to find a template for the given format
$template = new TemplateReference('FrameworkBundle', 'Exception', $name, $format, 'twig');
$template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig');
if ($templating->exists($template)) {
return $template;
}

// default to a generic HTML exception
$this->container->get('request')->setRequestFormat('html');

return new TemplateReference('FrameworkBundle', 'Exception', $name, 'html', 'twig');
return new TemplateReference('TwigBundle', 'Exception', $name, 'html', 'twig');
}
}
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

/**
* Registers the Twig exception listener if Twig is registered as a templating engine.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ExceptionListenerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (false === $container->hasDefinition('twig')) {

This comment has been minimized.

Copy link
@stof

stof Jul 21, 2011

Member

why checking for twig ? the relevant service in this compiler pass is twig.exception_listener

return;
}

// register the exception controller only if Twig is enabled
$engines = $container->getParameter('templating.engines');
if (!in_array('twig', $engines)) {
$container->removeDefinition('twig.exception_listener');
}
}
}
Expand Up @@ -32,6 +32,12 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('twig');

$rootNode
->children()
->scalarNode('exception_controller')->defaultValue('Symfony\\Bundle\\TwigBundle\\Controller\\ExceptionController::showAction')->end()
->end()
;

$this->addFormSection($rootNode);
$this->addGlobalsSection($rootNode);
$this->addTwigOptions($rootNode);
Expand Down
Expand Up @@ -39,6 +39,8 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('twig.exception_listener.controller', $config['exception_controller']);

$container->setParameter('twig.form.resources', $config['form']['resources']);
$container->getDefinition('twig.loader')->addMethodCall('addPath', array(__DIR__.'/../../../Bridge/Twig/Resources/views/Form'));

Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Expand Up @@ -16,6 +16,7 @@
<parameter key="twig.extension.routing.class">Symfony\Bridge\Twig\Extension\RoutingExtension</parameter>
<parameter key="twig.extension.yaml.class">Symfony\Bridge\Twig\Extension\YamlExtension</parameter>
<parameter key="twig.extension.form.class">Symfony\Bridge\Twig\Extension\FormExtension</parameter>
<parameter key="twig.exception_listener.class">Symfony\Component\HttpKernel\EventListener\ExceptionListener</parameter>
</parameters>

<services>
Expand Down Expand Up @@ -74,5 +75,12 @@
<tag name="twig.extension" />
<argument>%twig.form.resources%</argument>
</service>

<service id="twig.exception_listener" class="%twig.exception_listener.class%">
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-128" />
<tag name="monolog.logger" channel="request" />
<argument>%twig.exception_listener.controller%</argument>
<argument type="service" id="logger" on-invalid="null" />
</service>
</services>
</container>
@@ -0,0 +1 @@
{% include 'TwigBundle:Exception:error.xml.twig' with { 'exception': exception } %}
@@ -0,0 +1 @@
{% include 'TwigBundle:Exception:error.xml.twig' with { 'exception': exception } %}
@@ -0,0 +1 @@
{% include 'TwigBundle:Exception:exception.xml.twig' with { 'exception': exception } %}
@@ -0,0 +1,3 @@
/*
{% include 'TwigBundle:Exception:exception.txt.twig' with { 'exception': exception } %}
*/
Expand Up @@ -41,7 +41,7 @@
</div>

{% for position, e in exception.toarray %}
{% include 'FrameworkBundle:Exception:traces.html.twig' with { 'exception': e, 'position': position, 'count': previous_count } only %}
{% include 'TwigBundle:Exception:traces.html.twig' with { 'exception': e, 'position': position, 'count': previous_count } only %}
{% endfor %}

{% if logger %}
Expand All @@ -68,7 +68,7 @@
</div>

<div id="logs">
{% include 'FrameworkBundle:Exception:logs.html.twig' with { 'logs': logger.logs } only %}
{% include 'TwigBundle:Exception:logs.html.twig' with { 'logs': logger.logs } only %}
</div>

</div>
Expand Down
@@ -0,0 +1,3 @@
/*
{% include 'TwigBundle:Exception:exception.txt.twig' with { 'exception': exception } %}
*/
@@ -0,0 +1 @@
{% include 'TwigBundle:Exception:exception.xml.twig' with { 'exception': exception } %}
Expand Up @@ -2,6 +2,6 @@
[message] {{ exception.message }}
{% for i, e in exception.toarray %}
[{{ i + 1 }}] {{ e.class }}: {{ e.message }}
{% include 'FrameworkBundle:Exception:traces.txt.twig' with { 'exception': e } only %}
{% include 'TwigBundle:Exception:traces.txt.twig' with { 'exception': e } only %}

{% endfor %}
Expand Up @@ -3,7 +3,7 @@
<error code="{{ status_code }}" message="{{ status_text }}">
{% for e in exception.toarray %}
<exception class="{{ e.class }}" message="{{ e.message }}">
{% include 'FrameworkBundle:Exception:traces.xml.twig' with { 'exception': e } only %}
{% include 'TwigBundle:Exception:traces.xml.twig' with { 'exception': e } only %}
</exception>
{% endfor %}
</error>
@@ -1,9 +1,9 @@
{% extends 'FrameworkBundle::layout.html.twig' %}
{% extends 'TwigBundle::layout.html.twig' %}

{% block title %}
{{ exception.message }} ({{ status_code }} {{ status_text }})
{% endblock %}

{% block body %}
{% include 'FrameworkBundle:Exception:exception.html.twig' %}
{% include 'TwigBundle:Exception:exception.html.twig' %}
{% endblock %}
Expand Up @@ -18,7 +18,7 @@
<ol class="traces list_exception" id="traces_{{ position }}" style="display: {{ 0 == count ? 'block' : 'none' }}">
{% for i, trace in exception.trace %}
<li>
{% include 'FrameworkBundle:Exception:trace.html.twig' with { 'prefix': position, 'i': i, 'trace': trace } only %}
{% include 'TwigBundle:Exception:trace.html.twig' with { 'prefix': position, 'i': i, 'trace': trace } only %}
</li>
{% endfor %}
</ol>
Expand Down
@@ -1,6 +1,6 @@
{% if exception.trace|length %}
{% for trace in exception.trace %}
{% include 'FrameworkBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}
{% include 'TwigBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}

{% endfor %}
{% endif %}
@@ -1,7 +1,7 @@
<traces>
{% for trace in exception.trace %}
<trace>
{% include 'FrameworkBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}
{% include 'TwigBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}

</trace>
{% endfor %}
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/TwigBundle/TwigBundle.php
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigEnvironmentPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExceptionListenerPass;

/**
* Bundle.
Expand All @@ -27,5 +28,6 @@ public function build(ContainerBuilder $container)
parent::build($container);

$container->addCompilerPass(new TwigEnvironmentPass());
$container->addCompilerPass(new ExceptionListenerPass());
}
}
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\ExceptionController as BaseExceptionController;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;

/**
* ExceptionController.
Expand All @@ -33,7 +33,7 @@ public function showAction(FlattenException $exception, DebugLoggerInterface $lo
$code = $exception->getStatusCode();

return $this->container->get('templating')->renderResponse(
'FrameworkBundle:Exception:'.$template.'.html.twig',
'TwigBundle:Exception:'.$template.'.html.twig',
array(
'status_code' => $code,
'status_text' => Response::$statusTexts[$code],
Expand Down
@@ -1,4 +1,4 @@
{% extends 'FrameworkBundle::layout.html.twig' %}
{% extends 'TwigBundle::layout.html.twig' %}

{% block title 'Redirection Intercepted' %}

Expand Down

3 comments on commit 3749ad4

@stof
Copy link
Member

@stof stof commented on 3749ad4 Jul 21, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit means that exceptions are not handled at all in prod as the exception handler is enabled only in debug mode.

Thus, you need to document that overriding the error page by changing the template only works when you use TwigBundle.

@fabpot
Copy link
Member Author

@fabpot fabpot commented on 3749ad4 Jul 21, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already the case before.

@stof
Copy link
Member

@stof stof commented on 3749ad4 Jul 21, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but exceptions were handled by the exception listener in prod. So only build time exception were not handled in prod, and these one should not happen.

For someone that does not use TwigBundle, a NotFoundHttpException would result in a PHP Fatal Error in prod now.

Please sign in to comment.