Skip to content

Commit 4b9e1e4

Browse files
committed
bug #46956 [FrameworkBundle] Allow to specify null for exception mapping configuration values (andrew-demb)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [FrameworkBundle] Allow to specify `null` for exception mapping configuration values | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | | License | MIT | Doc PR | <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against the latest branch. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> `null` should be allowed because it is stated as allowed in the configuration info [1][2] Marked as bugfix due to an existing mismatch between config description and behavior. [1] https://github.com/symfony/symfony/blob/a2f27add28f7c2c61ef4cfb4e97833509ced0fc0/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php#L1219-L1220 [2] https://github.com/symfony/symfony/blob/a2f27add28f7c2c61ef4cfb4e97833509ced0fc0/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php#L1227-L1228 Commits ------- 1de8f3a9ff [FrameworkBundle] Allow to specify `null` for exception mapping configuration values
2 parents 394866c + 5694772 commit 4b9e1e4

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

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
]);

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>

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

Tests/DependencyInjection/FrameworkExtensionTest.php

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

0 commit comments

Comments
 (0)