Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dd6a939

Browse files
author
Alexey Deriyenko
committed
squashing
1 parent 4e7e429 commit dd6a939

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ public function load(array $configs, ContainerBuilder $container)
452452
$this->registerSecretsConfiguration($config['secrets'], $container, $loader);
453453

454454
$container->getDefinition('exception_listener')->replaceArgument(3, $config['exceptions']);
455+
$container->getDefinition('error_handler.error_renderer.html')->replaceArgument(6, $config['exceptions']);
455456

456457
if ($this->isConfigEnabled($container, $config['serializer'])) {
457458
if (!class_exists(\Symfony\Component\Serializer\Serializer::class)) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
->factory([HtmlErrorRenderer::class, 'getAndCleanOutputBuffer'])
3131
->args([service('request_stack')]),
3232
service('logger')->nullOnInvalid(),
33+
abstract_arg('an exceptions to log & status code mapping'),
3334
])
3435

3536
->alias('error_renderer.html', 'error_handler.error_renderer.html')

src/Symfony/Component/ErrorHandler/ErrorRenderer/HtmlErrorRenderer.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ class HtmlErrorRenderer implements ErrorRendererInterface
3939
private $projectDir;
4040
private $outputBuffer;
4141
private $logger;
42+
private $exceptionsMapping;
4243

4344
private static $template = 'views/error.html.php';
4445

4546
/**
46-
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
47+
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
4748
* @param string|FileLinkFormatter|null $fileLinkFormat
48-
* @param bool|callable $outputBuffer The output buffer as a string or a callable that should return it
49+
* @param bool|callable $outputBuffer The output buffer as a string or a callable that should return it
50+
* @param array $exceptionsMapping An exceptions to log & status code mapping
4951
*/
50-
public function __construct($debug = false, string $charset = null, $fileLinkFormat = null, string $projectDir = null, $outputBuffer = '', LoggerInterface $logger = null)
52+
public function __construct($debug = false, string $charset = null, $fileLinkFormat = null, string $projectDir = null, $outputBuffer = '', LoggerInterface $logger = null, array $exceptionsMapping = [])
5153
{
5254
if (!\is_bool($debug) && !\is_callable($debug)) {
5355
throw new \TypeError(sprintf('Argument 1 passed to "%s()" must be a boolean or a callable, "%s" given.', __METHOD__, \gettype($debug)));
@@ -63,6 +65,7 @@ public function __construct($debug = false, string $charset = null, $fileLinkFor
6365
$this->projectDir = $projectDir;
6466
$this->outputBuffer = $outputBuffer;
6567
$this->logger = $logger;
68+
$this->exceptionsMapping = $exceptionsMapping;
6669
}
6770

6871
/**
@@ -76,7 +79,14 @@ public function render(\Throwable $exception): FlattenException
7679
$headers['X-Debug-Exception-File'] = rawurlencode($exception->getFile()).':'.$exception->getLine();
7780
}
7881

79-
$exception = FlattenException::createFromThrowable($exception, null, $headers);
82+
$statusCode = 500;
83+
foreach ($this->exceptionsMapping as $class => $config) {
84+
if ($exception instanceof $class && $config['status_code']) {
85+
$statusCode = $config['status_code'];
86+
break;
87+
}
88+
}
89+
$exception = FlattenException::createFromThrowable($exception, $statusCode, $headers);
8090

8191
return $exception->setAsString($this->renderException($exception));
8292
}
@@ -262,8 +272,6 @@ private function formatFile(string $file, int $line, string $text = null): strin
262272
* @param string $file A file path
263273
* @param int $line The selected line number
264274
* @param int $srcContext The number of displayed lines around or -1 for the whole file
265-
*
266-
* @return string
267275
*/
268276
private function fileExcerpt(string $file, int $line, int $srcContext = 3): string
269277
{

src/Symfony/Component/ErrorHandler/Tests/ErrorRenderer/HtmlErrorRendererTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ public function getRenderData(): iterable
4040
<html>
4141
%A<title>An Error Occurred: Internal Server Error</title>
4242
%A<h2>The server returned a "500 Internal Server Error".</h2>%A
43+
HTML;
44+
45+
$expectedDebugWithStatusCode = <<<HTML
46+
<!-- Foo (418 I'm a teapot) -->
47+
<!DOCTYPE html>
48+
<html lang="en">
49+
%A<title>Foo (418 I'm a teapot)</title>
50+
%A<div class="trace trace-as-html" id="trace-box-1">%A
51+
<!-- Foo (418 I'm a teapot) -->
52+
HTML;
53+
54+
$expectedNonDebugWithStatusCode = <<<HTML
55+
<!DOCTYPE html>
56+
<html>
57+
%A<title>An Error Occurred: I'm a teapot</title>
58+
%A<h2>The server returned a "418 I'm a teapot".</h2>%A
4359
HTML;
4460

4561
yield '->render() returns the HTML content WITH stack traces in debug mode' => [
@@ -53,5 +69,27 @@ public function getRenderData(): iterable
5369
new HtmlErrorRenderer(false),
5470
$expectedNonDebug,
5571
];
72+
73+
yield '->render() returns the HTML content WITH stack traces in debug mode and contains the correct status code' => [
74+
new \RuntimeException('Foo'),
75+
new HtmlErrorRenderer(true, null, null, null, '', null, [
76+
\RuntimeException::class => [
77+
'status_code' => 418,
78+
'log_level' => null,
79+
],
80+
]),
81+
$expectedDebugWithStatusCode,
82+
];
83+
84+
yield '->render() returns the HTML content WITHOUT stack traces in non-debug mode and contains the correct status code' => [
85+
new \RuntimeException('Foo'),
86+
new HtmlErrorRenderer(false, null, null, null, '', null, [
87+
\RuntimeException::class => [
88+
'status_code' => 418,
89+
'log_level' => null,
90+
],
91+
]),
92+
$expectedNonDebugWithStatusCode,
93+
];
5694
}
5795
}

0 commit comments

Comments
 (0)