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 bb5934f

Browse filesBrowse files
committed
Add SapiErrorRendererSelector for context-based error rendering
1 parent 7a24bdc commit bb5934f
Copy full SHA for bb5934f

File tree

9 files changed

+92
-5
lines changed
Filter options

9 files changed

+92
-5
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')]),

‎src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ApiAttributesTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ApiAttributesTest.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public static function mapRequestPayloadProvider(): iterable
337337
'format' => 'dummy',
338338
'parameters' => [],
339339
'content' => 'Hello',
340-
'expectedResponse' => '415 Unsupported Media Type',
340+
'expectedResponse' => 'Unsupported format',
341341
'expectedStatusCode' => 415,
342342
];
343343

@@ -578,7 +578,7 @@ public static function mapRequestPayloadProvider(): iterable
578578
'format' => 'dummy',
579579
'parameters' => [],
580580
'content' => 'Hello',
581-
'expectedResponse' => '415 Unsupported Media Type',
581+
'expectedResponse' => 'Unsupported format',
582582
'expectedStatusCode' => 415,
583583
];
584584

@@ -824,7 +824,7 @@ public static function mapRequestPayloadProvider(): iterable
824824
'format' => 'dummy',
825825
'parameters' => [],
826826
'content' => 'Hello',
827-
'expectedResponse' => '415 Unsupported Media Type',
827+
'expectedResponse' => 'Unsupported format',
828828
'expectedStatusCode' => 415,
829829
];
830830

+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.