8000 bug #32232 [ErrorCatcher] some cleanup and better doc (Tobion) · symfony/symfony@7f6ed32 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 7f6ed32

Browse files
committed
bug #32232 [ErrorCatcher] some cleanup and better doc (Tobion)
This PR was merged into the 4.4 branch. Discussion ---------- [ErrorCatcher] some cleanup and better doc | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | | License | MIT | Doc PR | Commits ------- a609f57 [ErrorCatcher] some cleanup and better doc
2 parents b92e4ed + a609f57 commit 7f6ed32

File tree

10 files changed

+47
-47
lines changed

10 files changed

+47
-47
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/debug_prod.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<argument type="service" id="debug.file_link_formatter" />
2323
<argument>%kernel.debug%</argument>
2424
<argument>%kernel.charset%</argument>
25-
<argument type="service" id="error_handler.error_renderer" on-invalid="null" />
25+
<argument type="service" id="error_catcher.error_formatter" on-invalid="null" />
2626
</service>
2727

2828
<service id="debug.file_link_formatter" class="Symfony\Component\HttpKernel\Debug\FileLinkFormatter">

src/Symfony/Bundle/FrameworkBundle/Resources/config/error_catcher.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8-
<service id="error_catcher.error_renderer" class="Symfony\Component\ErrorCatcher\DependencyInjection\ErrorRenderer">
8+
<service id="error_catcher.error_formatter" class="Symfony\Component\ErrorCatcher\DependencyInjection\LazyLoadingErrorFormatter">
99
<argument /> <!-- error renderer locator -->
1010
</service>
1111

src/Symfony/Component/ErrorCatcher/DependencyInjection/ErrorCatcherPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ErrorCatcherPass implements CompilerPassInterface
2525
private $rendererService;
2626
private $rendererTag;
2727

28-
public function __construct(string $rendererService = 'error_catcher.error_renderer', string $rendererTag = 'error_catcher.renderer')
28+
public function __construct(string $rendererService = 'error_catcher.error_formatter', string $rendererTag = 'error_catcher.renderer')
2929
{
3030
$this->rendererService = $rendererService;
3131
$this->rendererTag = $rendererTag;

src/Symfony/Component/ErrorCatcher/DependencyInjection/ErrorRenderer.php renamed to src/Symfony/Component/ErrorCatcher/DependencyInjection/LazyLoadingErrorFormatter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
namespace Symfony\Component\ErrorCatcher\DependencyInjection;
1313

1414
use Psr\Container\ContainerInterface;
15-
use Symfony\Component\ErrorCatcher\ErrorRenderer\ErrorRenderer as BaseErrorRenderer;
15+
use Symfony\Component\ErrorCatcher\ErrorRenderer\ErrorFormatter;
1616

1717
/**
1818
* Lazily loads error renderers from the dependency injection container.
1919
*
2020
* @author Yonel Ceruto <yonelceruto@gmail.com>
2121
*/
22-
class ErrorRenderer extends BaseErrorRenderer
22+
class LazyLoadingErrorFormatter extends ErrorFormatter
2323
{
2424
private $container;
2525
private $initialized = [];

src/Symfony/Component/ErrorCatcher/ErrorRenderer/ErrorRenderer.php renamed to src/Symfony/Component/ErrorCatcher/ErrorRenderer/ErrorFormatter.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,38 @@
1515
use Symfony\Component\ErrorCatcher\Exception\FlattenException;
1616

1717
/**
18-
* Renders an Exception that represents a Response content.
18+
* Formats an exception to be used as response content.
19+
*
20+
* It delegates to implementations of ErrorRendererInterface depending on the format.
1921
*
2022
* @see ErrorRendererInterface
2123
*
2224
* @author Yonel Ceruto <yonelceruto@gmail.com>
2325
*/
24-
class ErrorRenderer
26+
class ErrorFormatter
2527
{
2628
private $renderers = [];
2729

2830
/**
2931
* @param ErrorRendererInterface[] $renderers
3032
*/
31-
public function __construct(array $renderers)
33+
public function __construct(iterable $renderers)
3234
{
3335
foreach ($renderers as $renderer) {
34-
if (!$renderer instanceof ErrorRendererInterface) {
35-
throw new \InvalidArgumentException(sprintf('Error renderer "%s" must implement "%s".', \get_class($renderer), ErrorRendererInterface::class));
36-
}
37-
38-
$this->addRenderer($renderer, $renderer::getFormat());
36+
$this->addRenderer($renderer);
3937
}
4038
}
4139

42-
public function addRenderer(ErrorRendererInterface $renderer, string $format): self
40+
/**
41+
* Registers an error renderer that is format specific.
42+
*
43+
* By passing an explicit format you can register a renderer for a different format than what
44+
* ErrorRendererInterface::getFormat() would return in order to register the same renderer for
45+
* several format aliases.
46+
*/
47+
public function addRenderer(ErrorRendererInterface $renderer, string $format = null): self
4348
{
44-
$this->renderers[$format] = $renderer;
49+
$this->renderers[$format ?? $renderer::getFormat()] = $renderer;
4550

4651
return $this;
4752
}

src/Symfony/Component/ErrorCatcher/ErrorRenderer/ErrorRendererInterface.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,19 @@
1414
use Symfony\Component\ErrorCatcher\Exception\FlattenException;
1515

1616
/**
17-
* Interface implemented by all error renderers.
17+
* Interface for classes that can render errors in a specific format.
1818
*
1919
* @author Yonel Ceruto <yonelceruto@gmail.com>
2020
*/
2121
interface ErrorRendererInterface
2222
{
2323
/**
24-
* Gets the format of the content.
25-
*
26-
* @return string The content format
24+
* Gets the format this renderer can return errors as.
2725
*/
2826
public static function getFormat(): string;
2927

3028
/**
31-
* Renders an Exception and returns the Response content.
32-
*
33-
* @return string The Response content as a string
29+
* Returns the response content of the rendered exception.
3430
*/
3531
public function render(FlattenException $exception): string;
3632
}

src/Symfony/Component/ErrorCatcher/Tests/DependencyInjection/ErrorCatcherPassTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
use Symfony\Component\DependencyInjection\Reference;
1818
use Symfony\Component\DependencyInjection\ServiceLocator;
1919
use Symfony\Component\ErrorCatcher\DependencyInjection\ErrorCatcherPass;
20-
use Symfony\Component\ErrorCatcher\DependencyInjection\ErrorRenderer;
20+
use Symfony\Component\ErrorCatcher\DependencyInjection\LazyLoadingErrorFormatter;
2121
use Symfony\Component\ErrorCatcher\ErrorRenderer\HtmlErrorRenderer;
2222
use Symfony\Component\ErrorCatcher\ErrorRenderer\JsonErrorRenderer;
2323

24-
class ErrorPassTest extends TestCase
24+
class ErrorCatcherPassTest extends TestCase
2525
{
2626
public function testProcess()
2727
{
2828
$container = new ContainerBuilder();
2929
$container->setParameter('kernel.debug', true);
30-
$definition = $container->register('error_catcher.error_renderer', ErrorRenderer::class)
30+
$definition = $container->register('error_catcher.error_formatter', LazyLoadingErrorFormatter::class)
3131
->addArgument([])
3232
;
3333
$container->register('error_catcher.renderer.html', HtmlErrorRenderer::class)

src/Symfony/Component/ErrorCatcher/Tests/DependencyInjection/ErrorRendererTest.php renamed to src/Symfony/Component/ErrorCatcher/Tests/DependencyInjection/LazyLoadingErrorFormatterTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,29 @@
1212
namespace Symfony\Component\ErrorCatcher\Tests\DependencyInjection;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\ErrorCatcher\DependencyInjection\ErrorRenderer;
15+
use Psr\Container\ContainerInterface;
16+
use Symfony\Component\ErrorCatcher\DependencyInjection\LazyLoadingErrorFormatter;
1617
use Symfony\Component\ErrorCatcher\ErrorRenderer\ErrorRendererInterface;
1718
use Symfony\Component\ErrorCatcher\Exception\FlattenException;
1819

19-
class ErrorRendererTest extends TestCase
20+
class LazyLoadingErrorFormatterTest extends TestCase
2021
{
2122
/**
2223
* @expectedException \Symfony\Component\ErrorCatcher\Exception\ErrorRendererNotFoundException
2324
* @expectedExceptionMessage No error renderer found for format "foo".
2425
*/
2526
public function testInvalidErrorRenderer()
2627
{
27-
$container = $this->getMockBuilder('Psr\Container\ContainerInterface')->getMock();
28+
$container = $this->getMockBuilder(ContainerInterface::class)->getMock();
2829
$container->expects($this->once())->method('has')->with('foo')->willReturn(false);
2930

3031
$exception = FlattenException::createFromThrowable(new \Exception('Foo'));
31-
(new ErrorRenderer($container))->render($exception, 'foo');
32+
(new LazyLoadingErrorFormatter($container))->render($exception, 'foo');
3233
}
3334

3435
public function testCustomErrorRenderer()
3536
{
36-
$container = $this->getMockBuilder('Psr\Container\ContainerInterface')->getMock();
37+
$container = $this->getMockBuilder(ContainerInterface::class)->getMock 10000 ();
3738
$container
3839
->expects($this->once())
3940
->method('has')
@@ -46,7 +47,7 @@ public function testCustomErrorRenderer()
4647
->willReturn(new FooErrorRenderer())
4748
;
4849

49-
$errorRenderer = new ErrorRenderer($container);
50+
$errorRenderer = new LazyLoadingErrorFormatter($container);
5051

5152
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
5253
$this->assertSame('Foo', $errorRenderer->render($exception, 'foo'));

src/Symfony/Component/ErrorCatcher/Tests/ErrorRenderer/ErrorRendererTest.php renamed to src/Symfony/Component/ErrorCatcher/Tests/ErrorRenderer/ErrorFormatterTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
namespace Symfony\Component\ErrorCatcher\Tests\ErrorRenderer;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\ErrorCatcher\ErrorRenderer\ErrorRenderer;
15+
use Symfony\Component\ErrorCatcher\ErrorRenderer\ErrorFormatter;
1616
use Symfony\Component\ErrorCatcher\ErrorRenderer\ErrorRendererInterface;
1717
use Symfony\Component\ErrorCatcher\Exception\FlattenException;
1818

19-
class ErrorRendererTest extends TestCase
19+
class ErrorFormatterTest extends TestCase
2020
{
2121
/**
2222
* @expectedException \Symfony\Component\ErrorCatcher\Exception\ErrorRendererNotFoundException
@@ -25,23 +25,21 @@ class ErrorRendererTest extends TestCase
2525
public function testErrorRendererNotFound()
2626
{
2727
$exception = FlattenException::createFromThrowable(new \Exception('foo'));
28-
(new ErrorRenderer([]))->render($exception, 'foo');
28+
(new ErrorFormatter([]))->render($exception, 'foo');
2929
}
3030

3131
/**
32-
* @expectedException \InvalidArgumentException
33-
* @expectedExceptionMessage Error renderer "stdClass" must implement "Symfony\Component\ErrorCatcher\ErrorRenderer\ErrorRendererInterface".
32+
* @expectedException \TypeError
3433
*/
3534
public function testInvalidErrorRenderer()
3635
{
37-
$exception = FlattenException::createFromThrowable(new \Exception('foo'));
38-
(new ErrorRenderer([new \stdClass()]))->render($exception, 'foo');
36+
new ErrorFormatter([new \stdClass()]);
3937
}
4038

4139
public function testCustomErrorRenderer()
4240
{
4341
$renderers = [new FooErrorRenderer()];
44-
$errorRenderer = new ErrorRenderer($renderers);
42+
$errorRenderer = new ErrorFormatter($renderers);
4543

4644
$exception = FlattenException::createFromThrowable(new \RuntimeException('Foo'));
4745
$this->assertSame('Foo', $errorRenderer->render($exception, 'foo'));

src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\Component\Console\Event\ConsoleEvent;
1717
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1818
use Symfony\Component\ErrorCatcher\ErrorHandler;
19-
use Symfony\Component\ErrorCatcher\ErrorRenderer\ErrorRenderer;
19+
use Symfony\Component\ErrorCatcher\ErrorRenderer\ErrorFormatter;
2020
use Symfony\Component\ErrorCatcher\ErrorRenderer\HtmlErrorRenderer;
2121
use Symfony\Component\ErrorCatcher\Exception\ErrorRendererNotFoundException;
2222
use Symfony\Component\ErrorCatcher\ExceptionHandler;
@@ -46,7 +46,7 @@ class DebugHandlersListener implements EventSubscriberInterface
4646
private $fileLinkFormat;
4747
private $scope;
4848
private $charset;
49-
private $errorRenderer;
49+
private $errorFormatter;
5050
private $firstCall = true;
5151
private $hasTerminatedWithException;
5252

@@ -59,7 +59,7 @@ class DebugHandlersListener implements EventSubscriberInterface
5959
* @param string|FileLinkFormatter|null $fileLinkFormat The format for links to source files
6060
* @param bool $scope Enables/disables scoping mode
6161
*/
62-
public function __construct(callable $exceptionHandler = null, LoggerInterface $logger = null, $levels = E_ALL, ?int $throwAt = E_ALL, bool $scream = true, $fileLinkFormat = null, bool $scope = true, string $charset = null, ErrorRenderer $errorRenderer = null)
62+
public function __construct(callable $exceptionHandler = null, LoggerInterface $logger = null, $levels = E_ALL, ?int $throwAt = E_ALL, bool $scream = true, $fileLinkFormat = null, bool $scope = true, string $charset = null, ErrorFormatter $errorFormatter = null)
6363
{
6464
$this->exceptionHandler = $exceptionHandler;
6565
$this->logger = $logger;
@@ -69,7 +69,7 @@ public function __construct(callable $exceptionHandler = null, LoggerInterface $
6969
$this->fileLinkFormat = $fileLinkFormat;
7070
$this->scope = $scope;
7171
$this->charset = $charset;
72-
$this->errorRenderer = $errorRenderer;
72+
$this->errorFormatter = $errorFormatter;
7373
}
7474

7575
/**
@@ -167,16 +167,16 @@ public function onKernelException(GetResponseForExceptionEvent $event)
167167

168168
$debug = $this->scream && $this->scope;
169169
$controller = function (Request $request) use ($debug) {
170-
if (null === $this->errorRenderer) {
171-
$this->errorRenderer = new ErrorRenderer([new HtmlErrorRenderer($debug, $this->charset, $this->fileLinkFormat)]);
170+
if (null === $this->errorFormatter) {
171+
$this->errorFormatter = new ErrorFormatter([new HtmlErrorRenderer($debug, $this->charset, $this->fileLinkFormat)]);
172172
}
173173

174174
$e = $request->attributes->get('exception');
175175

176176
try {
177-
return new Response($this->errorRenderer->render($e, $request->getRequestFormat()), $e->getStatusCode(), $e->getHeaders());
177+
return new Response($this->errorFormatter->render($e, $request->getRequestFormat()), $e->getStatusCode(), $e->getHeaders());
178178
} catch (ErrorRendererNotFoundException $_) {
179-
return new Response($this->errorRenderer->render($e), $e->getStatusCode(), $e->getHeaders());
179+
return new Response($this->errorFormatter->render($e), $e->getStatusCode(), $e->getHeaders());
180180
}
181181
};
182182

0 commit comments

Comments
 (0)
0