diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/error_renderer.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/error_renderer.php index 67f28ce44d838..ccb7eca6364ca 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/error_renderer.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/error_renderer.php @@ -11,7 +11,10 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; +use Symfony\Component\ErrorHandler\ErrorRenderer\CliErrorRenderer; +use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface; use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer; +use Symfony\Component\ErrorHandler\ErrorRenderer\SapiErrorRendererSelector; return static function (ContainerConfigurator $container) { $container->services() @@ -32,7 +35,18 @@ service('logger')->nullOnInvalid(), ]) + ->set('error_handler.error_renderer.cli', CliErrorRenderer::class) + + ->set('error_handler.error_renderer.default', ErrorRendererInterface::class) + ->factory([SapiErrorRendererSelector::class, 'select']) + ->args([ + service('error_renderer.cli'), + service('error_renderer.html'), + ]) + ->alias('error_renderer.html', 'error_handler.error_renderer.html') - ->alias('error_renderer', 'error_renderer.html') + ->alias('error_renderer.cli', 'error_handler.error_renderer.cli') + ->alias('error_renderer.default', 'error_handler.error_renderer.default') + ->alias('error_renderer', 'error_renderer.default') ; }; diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php index 535b95a399248..3ad0a8637fadc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php @@ -214,7 +214,7 @@ inline_service() ->factory([SerializerErrorRenderer::class, 'getPreferredFormat']) ->args([service('request_stack')]), - service('error_renderer.html'), + service('error_renderer.default'), inline_service() ->factory([HtmlErrorRenderer::class, 'isDebug']) ->args([service('request_stack'), param('kernel.debug')]), diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ApiAttributesTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ApiAttributesTest.php index 0dcfeaeba5ce2..f119300040f17 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ApiAttributesTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ApiAttributesTest.php @@ -337,7 +337,7 @@ public static function mapRequestPayloadProvider(): iterable 'format' => 'dummy', 'parameters' => [], 'content' => 'Hello', - 'expectedResponse' => '415 Unsupported Media Type', + 'expectedResponse' => 'Unsupported format', 'expectedStatusCode' => 415, ]; @@ -578,7 +578,7 @@ public static function mapRequestPayloadProvider(): iterable 'format' => 'dummy', 'parameters' => [], 'content' => 'Hello', - 'expectedResponse' => '415 Unsupported Media Type', + 'expectedResponse' => 'Unsupported format', 'expectedStatusCode' => 415, ]; @@ -824,7 +824,7 @@ public static function mapRequestPayloadProvider(): iterable 'format' => 'dummy', 'parameters' => [], 'content' => 'Hello', - 'expectedResponse' => '415 Unsupported Media Type', + 'expectedResponse' => 'Unsupported format', 'expectedStatusCode' => 415, ]; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ErrorHandlerWebTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ErrorHandlerWebTestCase.php new file mode 100644 index 0000000000000..b7ae921371ca4 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ErrorHandlerWebTestCase.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; + +class ErrorHandlerWebTestCase extends AbstractWebTestCase +{ + public function testNonHtmlErrorResponseOnCliContext() + { + $client = self::createClient(['test_case' => 'ErrorHandler', 'root_config' => 'config.yml', 'debug' => false]); + $client->request('GET', '/_error/500.html'); + + self::assertResponseStatusCodeSame(500, $client->getResponse()->getStatusCode()); + self::assertStringNotContainsString('', $client->getResponse()->getContent()); + self::assertStringContainsString('This is a sample exception.', $client->getResponse()->getContent()); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/ErrorHandler/bundles.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/ErrorHandler/bundles.php new file mode 100644 index 0000000000000..13ab9fddee4a6 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/ErrorHandler/bundles.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\Bundle\FrameworkBundle\FrameworkBundle; + +return [ + new FrameworkBundle(), +]; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/ErrorHandler/config.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/ErrorHandler/config.yml new file mode 100644 index 0000000000000..c9d2f72a5aeb7 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/ErrorHandler/config.yml @@ -0,0 +1,3 @@ +imports: + - { resource: ../config/default.yml } + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/ErrorHandler/routing.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/ErrorHandler/routing.yml new file mode 100644 index 0000000000000..9fe8323f57e5a --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/ErrorHandler/routing.yml @@ -0,0 +1,3 @@ +_errors: + resource: "@FrameworkBundle/Resources/config/routing/errors.xml" + prefix: /_error diff --git a/src/Symfony/Component/ErrorHandler/CHANGELOG.md b/src/Symfony/Component/ErrorHandler/CHANGELOG.md index cd8d07db6df36..8ca11f4db61c6 100644 --- a/src/Symfony/Component/ErrorHandler/CHANGELOG.md +++ b/src/Symfony/Component/ErrorHandler/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Add `error:dump` command + * Add `SapiErrorRendererSelector` to select the proper error renderer based on the current `PHP_SAPI` 7.1 --- diff --git a/src/Symfony/Component/ErrorHandler/ErrorRenderer/SapiErrorRendererSelector.php b/src/Symfony/Component/ErrorHandler/ErrorRenderer/SapiErrorRendererSelector.php new file mode 100644 index 0000000000000..aaa94e1dce5a3 --- /dev/null +++ b/src/Symfony/Component/ErrorHandler/ErrorRenderer/SapiErrorRendererSelector.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\ErrorHandler\ErrorRenderer; + +/** + * @author Yonel Ceruto + * + * @internal + */ +final class SapiErrorRendererSelector +{ + public static function select(ErrorRendererInterface $cliErrorRenderer, ErrorRendererInterface $htmlErrorRenderer): ErrorRendererInterface + { + return \in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? $cliErrorRenderer : $htmlErrorRenderer; + } +}