Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9f66d71

Browse filesBrowse files
committed
Add SapiErrorRendererSelector for context-based error rendering
1 parent 21b4dd7 commit 9f66d71
Copy full SHA for 9f66d71

File tree

8 files changed

+89
-2
lines changed
Filter options

8 files changed

+89
-2
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/error_renderer.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/error_renderer.php
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

14+
use Symfony\Component\ErrorHandler\ErrorRenderer\CliErrorRenderer;
15+
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
1416
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
17+
use Symfony\Component\ErrorHandler\ErrorRenderer\SapiErrorRendererSelector;
1518

1619
return static function (ContainerConfigurator $container) {
1720
$container->services()
@@ -32,7 +35,18 @@
3235
service('logger')->nullOnInvalid(),
3336
])
3437

38+
->set('error_handler.error_renderer.cli', CliErrorRenderer::class)
39+
40+
->set('error_handler.error_renderer.default', ErrorRendererInterface::class)
41+
->factory([SapiErrorRendererSelector::class, 'select'])
42+
->args([
43+
service('error_renderer.cli'),
44+
service('error_renderer.html'),
45+
])
46+
3547
->alias('error_renderer.html', 'error_handler.error_renderer.html')
36-
->alias('error_renderer', 'error_renderer.html')
48+
->alias('error_renderer.cli', 'error_handler.error_renderer.cli')
49+
->alias('error_renderer.default', 'error_handler.error_renderer.default')
50+
->alias('error_renderer', 'error_renderer.default')
3751
;
3852
};

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@
214214
inline_service()
215215
->factory([SerializerErrorRenderer::class, 'getPreferredFormat'])
216216
->args([service('request_stack')]),
217-
service('error_renderer.html'),
217+
service('error_renderer.default'),
218218
inline_service()
219219
->factory([HtmlErrorRenderer::class, 'isDebug'])
220220
->args([service('request_stack'), param('kernel.debug')]),
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
class ErrorHandlerWebTestCase extends AbstractWebTestCase
15+
{
16+
public function testNonHtmlErrorResponseOnCliContext()
17+
{
18+
$client = self::createClient(['test_case' => 'ErrorHandler', 'root_config' => 'config.yml', 'debug' => false]);
19+
$client->request('GET', '/_error/500.html');
20+
21+
self::assertResponseStatusCodeSame(500, $client->getResponse()->getStatusCode());
22+
self::assertStringNotContainsString('<!DOCTYPE html>', $client->getResponse()->getContent());
23+
self::assertStringContainsString('This is a sample exception.', $client->getResponse()->getContent());
24+
}
25+
}
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
14+
return [
15+
new FrameworkBundle(),
16+
];
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
_errors:
2+
resource: "@FrameworkBundle/Resources/config/routing/errors.xml"
3+
prefix: /_error

‎src/Symfony/Component/ErrorHandler/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/ErrorHandler/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `error:dump` command
8+
* Add `SapiErrorRendererSelector` to select the proper error renderer based on the current `PHP_SAPI`
89

910
7.1
1011
---
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ErrorHandler\ErrorRenderer;
13+
14+
/**
15+
* @author Yonel Ceruto <open@yceruto.dev>
16+
*
17+
* @internal
18+
*/
19+
final class SapiErrorRendererSelector
20+
{
21+
public static function select(ErrorRendererInterface $cliErrorRenderer, ErrorRendererInterface $htmlErrorRenderer): ErrorRendererInterface
22+
{
23+
return \in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? $cliErrorRenderer : $htmlErrorRenderer;
24+
}
25+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.