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

Skip to content

Commit 6a57d95

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

File tree

5 files changed

+62
-12
lines changed

5 files changed

+62
-12
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/ErrorRendererInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ interface ErrorRendererInterface
2323
/**
2424
* Renders a Throwable as a FlattenException.
2525
*/
26-
public function render(\Throwable $exception): FlattenException;
26+
public function render(\Throwable $throwable): FlattenException;
2727
}

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

Lines changed: 19 additions & 11 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,22 +65,30 @@ 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
/**
6972
* {@inheritdoc}
7073
*/
71-
public function render(\Throwable $exception): FlattenException
74+
public function render(\Throwable $throwable): FlattenException
7275
{
7376
$headers = ['Content-Type' => 'text/html; charset='.$this->charset];
74-
if (\is_bool($this->debug) ? $this->debug : ($this->debug)($exception)) {
75-
$headers['X-Debug-Exception'] = rawurlencode($exception->getMessage());
76-
$headers['X-Debug-Exception-File'] = rawurlencode($exception->getFile()).':'.$exception->getLine();
77+
if (\is_bool($this->debug) ? $this->debug : ($this->debug)($throwable)) {
78+
$headers['X-Debug-Exception'] = rawurlencode($throwable->getMessage());
79+
$headers['X-Debug-Exception-File'] = rawurlencode($throwable->getFile()).':'. $throwable->getLine();
7780
}
7881

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

81-
return $exception->setAsString($this->renderException($exception));
91+
return $throwable->setAsString($this->renderException($throwable));
8292
}
8393

8494
/**
@@ -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: 40 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,29 @@ 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+
[
78+
'status_code' => 418,
79+
'log_level' => NULL,
80+
],
81+
]),
82+
$expectedDebugWithStatusCode,
83+
];
84+
85+
yield '->render() returns the HTML content WITHOUT stack traces in non-debug mode and contains the correct status code' => [
86+
new \RuntimeException('Foo'),
87+
new HtmlErrorRenderer(false, null, null, null, '', null, [
88+
\RuntimeException::class =>
89+
[
90+
'status_code' => 418,
91+
'log_level' => NULL,
92+
],
93+
]),
94+
$expectedNonDebugWithStatusCode,
95+
];
5696
}
5797
}

0 commit comments

Comments
 (0)