Skip to content

Navigation Menu

Sign in
Appearance settings

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

[ErrorHandler] Add SapiErrorRendererSelector for context-based error rendering #60033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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')
;
};
Original file line number Diff line number Diff line change
Expand Up @@ -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')]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public static function mapRequestPayloadProvider(): iterable
'format' => 'dummy',
'parameters' => [],
'content' => 'Hello',
'expectedResponse' => '415 Unsupported Media Type',
'expectedResponse' => 'Unsupported format',
'expectedStatusCode' => 415,
];

Expand Down Expand Up @@ -578,7 +578,7 @@ public static function mapRequestPayloadProvider(): iterable
'format' => 'dummy',
'parameters' => [],
'content' => 'Hello',
'expectedResponse' => '415 Unsupported Media Type',
'expectedResponse' => 'Unsupported format',
'expectedStatusCode' => 415,
];

Expand Down Expand Up @@ -824,7 +824,7 @@ public static function mapRequestPayloadProvider(): iterable
'format' => 'dummy',
'parameters' => [],
'content' => 'Hello',
'expectedResponse' => '415 Unsupported Media Type',
'expectedResponse' => 'Unsupported format',
'expectedStatusCode' => 415,
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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('<!DOCTYPE html>', $client->getResponse()->getContent());
self::assertStringContainsString('This is a sample exception.', $client->getResponse()->getContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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(),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
imports:
- { resource: ../config/default.yml }

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_errors:
resource: "@FrameworkBundle/Resources/config/routing/errors.xml"
prefix: /_error
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/ErrorHandler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <open@yceruto.dev>
*
* @internal
*/
final class SapiErrorRendererSelector
{
public static function select(ErrorRendererInterface $cliErrorRenderer, ErrorRendererInterface $htmlErrorRenderer): ErrorRendererInterface
{
return \in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? $cliErrorRenderer : $htmlErrorRenderer;
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.