Skip to content

[Proposal][WIP][TwigBundle] Extract error handling into separate ErrorPageBundle #22450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"symfony/dom-crawler": "self.version",
"symfony/dotenv": "self.version",
"symfony/event-dispatcher": "self.version",
"symfony/error-page-bundle": "self.version",
"symfony/expression-language": "self.version",
"symfony/filesystem": "self.version",
"symfony/finder": "self.version",
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/ErrorPageBundle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Bundle/ErrorPageBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

4.0.0
-----

* Separated from TwigBundle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\TwigBundle\Controller;
namespace Symfony\Bundle\ErrorPageBundle\Controller;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
Expand Down Expand Up @@ -105,22 +105,22 @@ protected function findTemplate(Request $request, $format, $code, $showException

// For error pages, try to find a template for the specific HTTP status code and format
if (!$showException) {
$template = sprintf('@Twig/Exception/%s%s.%s.twig', $name, $code, $format);
$template = sprintf('@ErrorPage/Exception/%s%s.%s.twig', $name, $code, $format);
if ($this->templateExists($template)) {
return $template;
}
}

// try to find a template for the given format
$template = sprintf('@Twig/Exception/%s.%s.twig', $name, $format);
$template = sprintf('@ErrorPage/Exception/%s.%s.twig', $name, $format);
if ($this->templateExists($template)) {
return $template;
}

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

return sprintf('@Twig/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
return sprintf('@ErrorPage/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
}

// to be removed when the minimum required version of Twig is >= 3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\TwigBundle\Controller;
namespace Symfony\Bundle\ErrorPageBundle\Controller;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;
namespace Symfony\Bundle\ErrorPageBundle\DependencyInjection\Compiler;

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

/**
* Registers the Twig exception listener if Twig is registered as a templating engine.
* Registers the exception listener if Twig is registered as a templating engine.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
Expand All @@ -29,11 +29,11 @@ public function process(ContainerBuilder $container)

// register the exception controller only if Twig is enabled and required dependencies do exist
if (!class_exists('Symfony\Component\Debug\Exception\FlattenException') || !interface_exists('Symfony\Component\EventDispatcher\EventSubscriberInterface')) {
$container->removeDefinition('twig.exception_listener');
$container->removeDefinition('error_page.exception_listener');
} elseif ($container->hasParameter('templating.engines')) {
$engines = $container->getParameter('templating.engines');
if (!in_array('twig', $engines)) {
$container->removeDefinition('twig.exception_listener');
$container->removeDefinition('error_page.exception_listener');
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\ErrorPageBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* ErrorPageExtension configuration structure.
*
* @author Jeremy Mikola <jmikola@gmail.com>
*/
class Configuration implements ConfigurationInterface
{
/**
* Generates the configuration tree builder.
*
* @return TreeBuilder The tree builder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('error_page');

$rootNode
->children()
->scalarNode('exception_controller')->defaultValue('error_page.controller.exception:showAction')->end()
->end()
;

return $treeBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\ErrorPageBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* ErrorPageExtension.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jeremy Mikola <jmikola@gmail.com>
*/
class ErrorPageExtension extends Extension
{
/**
* @param array $configs
* @param ContainerBuilder $container
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('error_page.xml');

$configuration = $this->getConfiguration($configs, $container);

$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('error_page.exception_listener.controller', $config['exception_controller']);
}
}
31 changes: 31 additions & 0 deletions src/Symfony/Bundle/ErrorPageBundle/ErrorPageBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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\ErrorPageBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Bundle\ErrorPageBundle\DependencyInjection\Compiler\ExceptionListenerPass;

/**
* ErrorPageBundle
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ErrorPageBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new ExceptionListenerPass());
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Bundle/ErrorPageBundle/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2004-2017 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
10 changes: 10 additions & 0 deletions src/Symfony/Bundle/ErrorPageBundle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ErrorPageBundle
==========

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
25 changes: 25 additions & 0 deletions src/Symfony/Bundle/ErrorPageBundle/Resources/config/error_page.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="error_page.exception_listener" class="Symfony\Component\HttpKernel\EventListener\ExceptionListener">
<tag name="kernel.event_subscriber" />
<tag name="monolog.logger" channel="request" />
<argument>%error_page.exception_listener.controller%</argument>
<argument type="service" id="logger" on-invalid="null" />
</service>

<service id="error_page.controller.exception" class="Symfony\Bundle\ErrorPageBundle\Controller\ExceptionController">
<argument type="service" id="twig" />
<argument>%kernel.debug%</argument>
</service>

<service id="error_page.controller.preview_error" class="Symfony\Bundle\ErrorPageBundle\Controller\PreviewErrorController">
<argument type="service" id="http_kernel" />
<argument>%error_page.exception_listener.controller%</argument>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ include('@ErrorPage/Exception/error.xml.twig') }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ include('@ErrorPage/Exception/error.xml.twig') }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ include('@ErrorPage/Exception/exception.xml.twig', { exception: exception }) }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
{{ include('@ErrorPage/Exception/exception.txt.twig', { exception: exception }) }}
*/
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<h2 class="exception-hierarchy">
{% for previousException in exception.allPrevious|reverse %}
{{ previousException.class|abbr_class }}
<span class="icon">{{ include('@Twig/images/chevron-right.svg') }}</span>
<span class="icon">{{ include('@ErrorPage/images/chevron-right.svg') }}</span>
{% endfor %}
{{ exception.class|abbr_class }}
</h2>
Expand All @@ -20,7 +20,7 @@
</h1>

<div class="exception-illustration hidden-xs-down">
{{ include('@Twig/images/symfony-ghost.svg') }}
{{ include('@ErrorPage/images/symfony-ghost.svg') }}
</div>
</div>
</div>
Expand Down Expand Up @@ -48,7 +48,7 @@

<div class="tab-content">
{% for i, e in exception_as_array %}
{{ include('@Twig/Exception/traces.html.twig', { exception: e, index: loop.index, expand: i in _exceptions_with_user_code or (_exceptions_with_user_code is empty and loop.first) }, with_context = false) }}
{{ include('@ErrorPage/Exception/traces.html.twig', { exception: e, index: loop.index, expand: i in _exceptions_with_user_code or (_exceptions_with_user_code is empty and loop.first) }, with_context = false) }}
{% endfor %}
</div>
</div>
Expand All @@ -61,7 +61,7 @@

<div class="tab-content">
{% if logger %}
{{ include('@Twig/Exception/logs.html.twig', { logs: logger.logs }, with_context = false) }}
{{ include('@ErrorPage/Exception/logs.html.twig', { logs: logger.logs }, with_context = false) }}
{% else %}
<div class="empty">
<p>No log messages</p>
Expand All @@ -81,7 +81,7 @@

<div class="tab-content">
{% for e in exception_as_array %}
{{ include('@Twig/Exception/traces_text.html.twig', { exception: e, index: loop.index, num_exceptions: loop.length }, with_context = false) }}
{{ include('@ErrorPage/Exception/traces_text.html.twig', { exception: e, index: loop.index, num_exceptions: loop.length }, with_context = false) }}
{% endfor %}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
{{ include('@ErrorPage/Exception/exception.txt.twig', { exception: exception }) }}
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ include('@ErrorPage/Exception/exception.xml.twig', { exception: exception }) }}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
[message] {{ exception.message }}
{% for i, e in exception.toarray %}
[{{ i + 1 }}] {{ e.class }}: {{ e.message }}
{{ include('@Twig/Exception/traces.txt.twig', { exception: e }, with_context = false) }}
{{ include('@ErrorPage/Exception/traces.txt.twig', { exception: e }, with_context = false) }}

{% endfor %}
Original file line number Diff line number Diff line change
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('@Twig/Exception/traces.xml.twig', { exception: e }, with_context = false) }}
{{ include('@ErrorPage/Exception/traces.xml.twig', { exception: e }, with_context = false) }}
</exception>
{% endfor %}
</error>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends '@Twig/layout.html.twig' %}
{% extends '@ErrorPage/layout.html.twig' %}

{% block head %}
<style>
Expand Down Expand Up @@ -138,5 +138,5 @@
{% endblock %}

{% block body %}
{% include '@Twig/Exception/exception.html.twig' %}
{% include '@ErrorPage/Exception/exception.html.twig' %}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% if trace.file|default(false) %}
<span class="icon icon-close">{{ include('@Twig/images/icon-minus-square.svg') }}</span>
<span class="icon icon-open">{{ include('@Twig/images/icon-plus-square.svg') }}</span>
<span class="icon icon-close">{{ include('@ErrorPage/images/icon-minus-square.svg') }}</span>
<span class="icon icon-open">{{ include('@ErrorPage/images/icon-plus-square.svg') }}</span>
{% endif %}

{% if trace.function %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</span>
{{ exception.class|split('\\')|last }}

<span class="icon icon-close">{{ include('@Twig/images/icon-minus-square-o.svg') }}</span>
<span class="icon icon-open">{{ include('@Twig/images/icon-plus-square-o.svg') }}</span>
<span class="icon icon-close">{{ include('@ErrorPage/images/icon-minus-square-o.svg') }}</span>
<span class="icon icon-open">{{ include('@ErrorPage/images/icon-plus-square-o.svg') }}</span>
</h3>

{% if exception.message is not empty and index > 1 %}
Expand All @@ -28,7 +28,7 @@
{% if _display_code_snippet %}{% set _is_first_user_code = false %}{% endif %}
<tr>
<td class="trace-line {{ trace.file|default(false) ? 'sf-toggle' }}" data-toggle-selector="#trace-html-{{ index }}-{{ i }}" data-toggle-initial="{{ _display_code_snippet ? 'display' }}">
{{ include('@Twig/Exception/trace.html.twig', { prefix: index, i: i, trace: trace }, with_context = false) }}
{{ include('@ErrorPage/Exception/trace.html.twig', { prefix: index, i: i, trace: trace }, with_context = false) }}
</td>
</tr>
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% endif %}

{% for trace in exception.trace %}
at {{ include('@Twig/Exception/trace.txt.twig', { trace: trace }, with_context = false) }}
at {{ include('@ErrorPage/Exception/trace.txt.twig', { trace: trace }, with_context = false) }}
{% endfor %}
</pre>
{% endif %}
Loading