Skip to content

Add ErrorHandler component #31065

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

Merged
merged 1 commit into from
Jun 27, 2019
Merged
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
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\ErrorHandler\Exception\FatalThrowableError;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('web.xml');
$loader->load('services.xml');
$loader->load('fragment_renderer.xml');
$loader->load('error_renderer.xml');

$container->registerAliasForArgument('parameter_bag', PsrContainerInterface::class);

Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
use Symfony\Component\Cache\DependencyInjection\CachePoolPrunerPass;
use Symfony\Component\Config\Resource\ClassExistenceResource;
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Compiler\RegisterReverseContainerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\ErrorHandler\DependencyInjection\ErrorHandlerPass;
use Symfony\Component\ErrorHandler\ErrorHandler;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\Form\DependencyInjection\FormPass;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -90,6 +91,7 @@ public function build(ContainerBuilder $container)
KernelEvents::FINISH_REQUEST,
];

$this->addCompilerPassIfExists($container, ErrorHandlerPass::class);
$container->addCompilerPass(new LoggerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
$container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());
$container->addCompilerPass(new RemoveEmptyControllerArgumentLocatorsPass(), PassConfig::TYPE_BEFORE_REMOVING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
<argument>null</argument><!-- Log levels map for enabled error levels -->
<argument>%debug.error_handler.throw_at%</argument>
<argument>%kernel.debug%</argument>
<argument type="service" id="debug.file_link_formatter"></argument>
<argument type="service" id="debug.file_link_formatter" />
<argument>%kernel.debug%</argument>
<argument>%kernel.charset%</argument>
<argument type="service" id="error_handler.error_renderer" on-invalid="null" />
</service>

<service id="debug.file_link_formatter" class="Symfony\Component\HttpKernel\Debug\FileLinkFormatter">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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 https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<defaults public="false" />

<service id="error_handler.error_renderer" class="Symfony\Component\ErrorHandler\DependencyInjection\ErrorRenderer">
<argument /> <!-- error renderer locator -->
</service>

<service id="error_handler.renderer.html" class="Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer">
<tag name="error_handler.renderer" />
<argument>%kernel.debug%</argument>
<argument>%kernel.charset%</argument>
<argument>%debug.file_link_format%</argument>
</service>

<service id="error_handler.renderer.json" class="Symfony\Component\ErrorHandler\ErrorRenderer\JsonErrorRenderer">
<tag name="error_handler.renderer" />
<argument>%kernel.debug%</argument>
</service>

<service id="error_handler.renderer.xml" class="Symfony\Component\ErrorHandler\ErrorRenderer\XmlErrorRenderer">
<tag name="error_handler.renderer" format="atom" />
<tag name="error_handler.renderer" />
<argument>%kernel.debug%</argument>
<argument>%kernel.charset%</argument>
</service>

<service id="error_handler.renderer.txt" class="Symfony\Component\ErrorHandler\ErrorRenderer\TxtErrorRenderer">
<tag name="error_handler.renderer" />
<argument>%kernel.debug%</argument>
<argument>%kernel.charset%</argument>
</service>
</services>
</container>
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"symfony/config": "^4.2|^5.0",
"symfony/dependency-injection": "^4.4|^5.0",
"symfony/http-foundation": "^4.3|^5.0",
"symfony/http-kernel": "^4.3|^5.0",
"symfony/http-kernel": "^4.4|^5.0",
"symfony/polyfill-mbstring": "~1.0",
"symfony/filesystem": "^3.4|^4.0|^5.0",
"symfony/finder": "^3.4|^4.0|^5.0",
Expand Down Expand Up @@ -69,6 +69,7 @@
"symfony/asset": "<3.4",
"symfony/browser-kit": "<4.3",
"symfony/console": "<4.3",
"symfony/debug": "<4.4",
"symfony/dotenv": "<4.2",
"symfony/dom-crawler": "<4.3",
"symfony/form": "<4.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Bundle\TwigBundle\Controller;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Bundle\TwigBundle\Controller;

use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ 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')) {
if (!class_exists('Symfony\Component\ErrorHandler\Exception\FlattenException') || !interface_exists('Symfony\Component\EventDispatcher\EventSubscriberInterface')) {
$container->removeDefinition('twig.exception_listener');
} elseif ($container->hasParameter('templating.engines')) {
$engines = $container->getParameter('templating.engines');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Bundle\TwigBundle\Controller\ExceptionController;
use Symfony\Bundle\TwigBundle\Tests\TestCase;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Bundle\TwigBundle\Controller\PreviewErrorController;
use Symfony\Bundle\TwigBundle\Tests\TestCase;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"symfony/config": "^4.2|^5.0",
"symfony/twig-bridge": "^4.4|^5.0",
"symfony/http-foundation": "^4.3|^5.0",
"symfony/http-kernel": "^4.1|^5.0",
"symfony/http-kernel": "^4.4|^5.0",
"symfony/polyfill-ctype": "~1.8",
"twig/twig": "~1.41|~2.10"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Bundle\WebProfilerBundle\Controller;

use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand All @@ -30,14 +30,18 @@ class ExceptionController
protected $twig;
protected $debug;
protected $profiler;
private $fileLinkFormat;
private $errorRenderer;

public function __construct(Profiler $profiler = null, Environment $twig, bool $debug, FileLinkFormatter $fileLinkFormat = null)
public function __construct(Profiler $profiler = null, Environment $twig, bool $debug, FileLinkFormatter $fileLinkFormat = null, HtmlErrorRenderer $errorRenderer = null)
{
$this->profiler = $profiler;
$this->twig = $twig;
$this->debug = $debug;
$this->fileLinkFormat = $fileLinkFormat;
$this->errorRenderer = $errorRenderer;

if (null === $errorRenderer) {
$this->errorRenderer = new HtmlErrorRenderer($debug, $this->twig->getCharset(), $fileLinkFormat);
}
}

/**
Expand All @@ -61,9 +65,7 @@ public function showAction($token)
$template = $this->getTemplate();

if (!$this->twig->getLoader()->exists($template)) {
$handler = new ExceptionHandler($this->debug, $this->twig->getCharset(), $this->fileLinkFormat);

return new Response($handler->getContent($exception), 200, ['Content-Type' => 'text/html']);
return new Response($this->errorRenderer->getBody($exception), 200, ['Content-Type' => 'text/html']);
}

$code = $exception->getStatusCode();
Expand Down Expand Up @@ -97,13 +99,10 @@ public function cssAction($token)

$this->profiler->disable();

$exception = $this->profiler->loadProfile($token)->getCollector('exception')->getException();
$template = $this->getTemplate();

if (!$this->templateExists($template)) {
$handler = new ExceptionHandler($this->debug, $this->twig->getCharset(), $this->fileLinkFormat);

return new Response($handler->getStylesheet($exception), 200, ['Content-Type' => 'text/css']);
return new Response($this->errorRenderer->getStylesheet(), 200, ['Content-Type' => 'text/css']);
}

return new Response($this->twig->render('@WebProfiler/Collector/exception.css.twig'), 200, ['Content-Type' => 'text/css']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<argument type="service" id="twig" />
<argument>%kernel.debug%</argument>
<argument type="service" id="debug.file_link_formatter" />
<argument type="service" id="error_handler.renderer.html" on-invalid="null" />
</service>

<service id="web_profiler.csp.handler" class="Symfony\Bundle\WebProfilerBundle\Csp\ContentSecurityPolicyHandler">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\EventDispatcher\EventDispatcher;

Expand Down Expand Up @@ -53,6 +54,7 @@ protected function setUp()
$this->kernel = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\KernelInterface')->getMock();

$this->container = new ContainerBuilder();
$this->container->register('error_handler.renderer.html', HtmlErrorRenderer::class);
$this->container->register('event_dispatcher', EventDispatcher::class)->setPublic(true);
$this->container->register('router', $this->getMockClass('Symfony\\Component\\Routing\\RouterInterface'))->setPublic(true);
$this->container->register('twig', 'Twig\Environment')->setPublic(true);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/WebProfilerBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": "^7.1.3",
"symfony/config": "^4.2|^5.0",
"symfony/http-kernel": "^4.3",
"symfony/http-kernel": "^4.4",
"symfony/routing": "^3.4|^4.0|^5.0",
"symfony/twig-bundle": "^4.2|^5.0",
"symfony/var-dumper": "^3.4|^4.0|^5.0",
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\ErrorHandler\ErrorHandler;
use Symfony\Component\ErrorHandler\Exception\FatalThrowableError;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;

Expand Down
24 changes: 5 additions & 19 deletions src/Symfony/Component/Debug/BufferingLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,13 @@

namespace Symfony\Component\Debug;

use Psr\Log\AbstractLogger;
use Symfony\Component\ErrorHandler\BufferingLogger as BaseBufferingLogger;

@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.4, use "%s" instead.', BufferingLogger::class, BaseBufferingLogger::class), E_USER_DEPRECATED);

/**
* A buffering logger that stacks logs for later.
*
* @author Nicolas Grekas <p@tchwork.com>
* @deprecated since Symfony 4.4, use Symfony\Component\ErrorHandler\BufferingLogger instead.
*/
class BufferingLogger extends AbstractLogger
class BufferingLogger extends BaseBufferingLogger
{
private $logs = [];

public function log($level, $message, array $context = [])
{
$this->logs[] = [$level, $message, $context];
}

public function cleanLogs()
{
$logs = $this->logs;
$this->logs = [];

return $logs;
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/Debug/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
CHANGELOG
=========

4.4.0
-----

* deprecated the `BufferingLogger`, `ErrorHandler` and `ExceptionHandler` classes,
they have been moved to the `ErrorHandler` component
* deprecated the `FatalErrorHandlerInterface`, `ClassNotFoundFatalErrorHandler`,
`UndefinedFunctionFatalErrorHandler` and `UndefinedMethodFatalErrorHandler` classes,
they have been moved to the `ErrorHandler` component
* deprecated the `ClassNotFoundException`, `FatalErrorException`, `FatalThrowableError`,
`FlattenException`, `OutOfMemoryException`, `SilencedErrorContext`, `UndefinedFunctionException`,
and `UndefinedMethodException`, they have been moved to the `ErrorHandler` component

4.3.0
-----

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Debug/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace Symfony\Component\Debug;

use Symfony\Component\ErrorHandler\BufferingLogger;
use Symfony\Component\ErrorHandler\ErrorHandler;
use Symfony\Component\ErrorHandler\ExceptionHandler;

/**
* Registers all the debug tools.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Debug/DebugClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function getClassLoader()
public static function enable()
{
// Ensures we don't hit https://bugs.php.net/42098
class_exists('Symfony\Component\Debug\ErrorHandler');
class_exists('Symfony\Component\ErrorHandler\ErrorHandler');
class_exists('Psr\Log\LogLevel');

if (!\is_array($functions = spl_autoload_functions())) {
Expand Down
Loading