8000 [FrameworkBundle] Allow to specify `null` for exception mapping confi… · symfony/symfony@1de8f3a · GitHub
[go: up one dir, main page]

Skip to content

Commit 1de8f3a

Browse files
andrew-dembfabpot
authored andcommitted
[FrameworkBundle] Allow to specify null for exception mapping configuration values
1 parent 00037d8 commit 1de8f3a

File tree

6 files changed

+52
-3
lines changed

6 files changed

+52
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,15 +1227,19 @@ private function addExceptionsSection(ArrayNodeDefinition $rootNode)
12271227
->scalarNode('log_level')
12281228
->info('The level of log message. Null to let Symfony decide.')
12291229
->validate()
1230-
->ifTrue(function ($v) use ($logLevels) { return !\in_array($v, $logLevels); })
1230+
->ifTrue(function ($v) use ($logLevels) { return null !== $v && !\in_array($v, $logLevels, true); })
12311231
->thenInvalid(sprintf('The log level is not valid. Pick one among "%s".', implode('", "', $logLevels)))
12321232
->end()
12331233
->defaultNull()
12341234
->end()
12351235
->scalarNode('status_code')
1236-
->info('The status code of the response. Null to let Symfony decide.')
1236+
->info('The status code of the response. Null or 0 to let Symfony decide.')
1237+
->beforeNormalization()
1238+
->ifTrue(function ($v) { return 0 === $v; })
1239+
->then(function ($v) { return null; })
1240+
->end()
12371241
->validate()
1238-
->ifTrue(function ($v) { return $v < 100 || $v > 599; })
1242+
->ifTrue(function ($v) { return null !== $v && ($v < 100 || $v > 599); })
12391243
->thenInvalid('The status code is not valid. Pick a value between 100 and 599.')
12401244
->end()
12411245
->defaultNull()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
<?php
22

33
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
4+
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
5+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
6+
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
47

58
$container->loadFromExtension('framework', [
69
'exceptions' => [
710
BadRequestHttpException::class => [
811
'log_level' => 'info',
912
'status_code' => 422,
1013
],
14+
NotFoundHttpException::class => [
15+
'log_level' => 'info',
16+
'status_code' => null,
17+
],
18+
ConflictHttpException::class => [
19+
'log_level' => 'info',
20+
'status_code' => 0,
21+
],
22+
ServiceUnavailableHttpException::class => [
23+
'log_level' => null,
24+
'status_code' => 500,
25+
],
1126
],
1227
]);

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/exceptions.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<framework:config>
99
<framework:exceptions>
1010
<framework:exception name="Symfony\Component\HttpKernel\Exception\BadRequestHttpException" log-level="info" status-code="422" />
11+
<framework:exception name="Symfony\Component\HttpKernel\Exception\NotFoundHttpException" log-level="info" status-code="0" />
12+
<framework:exception name="Symfony\Component\HttpKernel\Exception\ConflictHttpException" log-level="info" status-code="0" />
13+
<framework:exception name="Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException" log-level="null" status-code="500" />
1114
</framework:exceptions>
1215
</framework:config>
1316
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/exceptions.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@ framework:
33
Symfony\Component\HttpKernel\Exception\BadRequestHttpException:
44
log_level: info
55
status_code: 422
6+
Symfony\Component\HttpKernel\Exception\NotFoundHttpException:
7+
log_level: info
8+
status_code: null
9+
Symfony\Component\HttpKernel\Exception\ConflictHttpException:
10+
log_level: info
11+
status_code: 0
12+
Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException:
13+
log_level: null
14+
status_code: 500

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,18 @@ public function testExceptionsConfig()
550550
'log_level' => 'info',
551551
'status_code' => 422,
552552
],
553+
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class => [
554+
'log_level' => 'info',
555+
'status_code' => null,
556+
],
557+
\Symfony\Component\HttpKernel\Exception\ConflictHttpException::class => [
558+
'log_level' => 'info',
559+
'status_code' => null,
560+
],
561+
\Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException::class => [
562+
'log_level' => null,
563+
'status_code' => 500,
564+
],
553565
], $container->getDefinition('exception_listener')->getArgument(3));
554566
}
555567

src/Symfony/Component/HttpKernel/EventListener/ErrorListener.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ class ErrorListener implements EventSubscriberInterface
3333
protected $controller;
3434
protected $logger;
3535
protected $debug;
36+
/**
37+
* @var array<class-string, array{log_level: string|null, status_code: int<100,599>|null}>
38+
*/
3639
protected $exceptionsMapping;
3740

41+
/**
42+
* @param array<class-string, array{log_level: string|null, status_code: int<100,599>|null}> $exceptionsMapping
43+
*/
3844
public function __construct($controller, LoggerInterface $logger = null, bool $debug = false, array $exceptionsMapping = [])
3945
{
4046
$this->controller = $controller;

0 commit comments

Comments
 (0)
0