Skip to content

Commit a8527d4

Browse files
Merge branch '7.4' into 8.0
* 7.4: [ErrorHandler][FrameworkBundle] Add support for selecting the appropriate error renderer based on the `APP_RUNTIME_MODE`
2 parents f6d6856 + 0681ef3 commit a8527d4

File tree

9 files changed

+105
-2
lines changed

9 files changed

+105
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ CHANGELOG
3939
* Add `framework.allowed_http_method_override` option
4040
* Initialize `router.request_context`'s `_locale` parameter to `%kernel.default_locale%`
4141
* Deprecate `ConfigBuilderCacheWarmer`, return PHP arrays from your config instead
42+
* Add support for selecting the appropriate error renderer based on the `APP_RUNTIME_MODE` env var
4243

4344
7.3
4445
---
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\ErrorHandler\ErrorRenderer;
13+
14+
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
15+
16+
/**
17+
* @internal
18+
*
19+
* @author Yonel Ceruto <open@yceruto.dev>
20+
*/
21+
final class RuntimeModeErrorRendererSelector
22+
{
23+
/**
24+
* @param \Closure(): ErrorRendererInterface $htmlErrorRenderer
25+
* @param \Closure(): ErrorRendererInterface $cliErrorRenderer
26+
*/
27+
public static function select(bool $isWebMode, \Closure $htmlErrorRenderer, \Closure $cliErrorRenderer): ErrorRendererInterface
28+
{
29+
return ($isWebMode ? $htmlErrorRenderer : $cliErrorRenderer)();
30+
}
31+
}

Resources/config/error_renderer.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

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

14+
use Symfony\Bundle\FrameworkBundle\ErrorHandler\ErrorRenderer\RuntimeModeErrorRendererSelector;
15+
use Symfony\Component\ErrorHandler\ErrorRenderer\CliErrorRenderer;
16+
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
1417
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
1518

1619
return static function (ContainerConfigurator $container) {
@@ -32,7 +35,19 @@
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([RuntimeModeErrorRendererSelector::class, 'select'])
42+
->args([
43+
param('kernel.runtime_mode.web'),
44+
service_closure('error_renderer.html'),
45+
service_closure('error_renderer.cli'),
46+
])
47+
3548
->alias('error_renderer.html', 'error_handler.error_renderer.html')
36-
->alias('error_renderer', 'error_renderer.html')
49+
->alias('error_renderer.cli', 'error_handler.error_renderer.cli')
50+
->alias('error_renderer.default', 'error_handler.error_renderer.default')
51+
->alias('error_renderer', 'error_renderer.default')
3752
;
3853
};

Resources/config/serializer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Cache\CacheItemPoolInterface;
1515
use Symfony\Bundle\FrameworkBundle\CacheWarmer\SerializerCacheWarmer;
1616
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
17+
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
1718
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
1819
use Symfony\Component\ErrorHandler\ErrorRenderer\SerializerErrorRenderer;
1920
use Symfony\Component\PropertyInfo\Extractor\SerializerExtractor;
@@ -218,7 +219,10 @@
218219
inline_service()
219220
->factory([SerializerErrorRenderer::class, 'getPreferredFormat'])
220221
->args([service('request_stack')]),
221-
service('error_renderer.html'),
222+
inline_service(ErrorRendererInterface::class)
223+
->factory([\Closure::class, 'fromCallable'])
224+
->args([service('error_renderer.default'), 'render'])
225+
->lazy(),
222226
inline_service()
223227
->factory([HtmlErrorRenderer::class, 'isDebug'])
224228
->args([service('request_stack'), param('kernel.debug')]),
Lines 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 testHtmlErrorResponseOnCliContext()
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::assertStringContainsString('<!DOCTYPE html>', $client->getResponse()->getContent());
23+
self::assertStringContainsString('Oops! An Error Occurred', $client->getResponse()->getContent());
24+
}
25+
}

Tests/Functional/app/ApiAttributesTest/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
imports:
22
- { resource: ../config/default.yml }
33

4+
parameters:
5+
kernel.runtime_mode.web: true
6+
47
framework:
58
serializer:
69
enabled: true
Lines 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+
];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
parameters:
5+
kernel.runtime_mode.web: true
Lines 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

0 commit comments

Comments
 (0)