Skip to content

Commit

Permalink
bug #46956 [FrameworkBundle] Allow to specify null for exception ma…
Browse files Browse the repository at this point in the history
…pping 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
  • Loading branch information
fabpot committed Sep 30, 2022
2 parents 394866c + 5694772 commit 4b9e1e4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
10 changes: 7 additions & 3 deletions DependencyInjection/Configuration.php
Expand Up @@ -1227,15 +1227,19 @@ private function addExceptionsSection(ArrayNodeDefinition $rootNode)
->scalarNode('log_level')
->info('The level of log message. Null to let Symfony decide.')
->validate()
->ifTrue(function ($v) use ($logLevels) { return !\in_array($v, $logLevels); })
->ifTrue(function ($v) use ($logLevels) { return null !== $v && !\in_array($v, $logLevels, true); })
->thenInvalid(sprintf('The log level is not valid. Pick one among "%s".', implode('", "', $logLevels)))
->end()
->defaultNull()
->end()
->scalarNode('status_code')
->info('The status code of the response. Null to let Symfony decide.')
->info('The status code of the response. Null or 0 to let Symfony decide.')
->beforeNormalization()
->ifTrue(function ($v) { return 0 === $v; })
->then(function ($v) { return null; })
->end()
->validate()
->ifTrue(function ($v) { return $v < 100 || $v > 599; })
->ifTrue(function ($v) { return null !== $v && ($v < 100 || $v > 599); })
->thenInvalid('The status code is not valid. Pick a value between 100 and 599.')
->end()
->defaultNull()
Expand Down
15 changes: 15 additions & 0 deletions Tests/DependencyInjection/Fixtures/php/exceptions.php
@@ -1,12 +1,27 @@
<?php

use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;

$container->loadFromExtension('framework', [
'exceptions' => [
BadRequestHttpException::class => [
'log_level' => 'info',
'status_code' => 422,
],
NotFoundHttpException::class => [
'log_level' => 'info',
'status_code' => null,
],
ConflictHttpException::class => [
'log_level' => 'info',
'status_code' => 0,
],
ServiceUnavailableHttpException::class => [
'log_level' => null,
'status_code' => 500,
],
],
]);
3 changes: 3 additions & 0 deletions Tests/DependencyInjection/Fixtures/xml/exceptions.xml
Expand Up @@ -8,6 +8,9 @@
<framework:config>
<framework:exceptions>
<framework:exception name="Symfony\Component\HttpKernel\Exception\BadRequestHttpException" log-level="info" status-code="422" />
<framework:exception name="Symfony\Component\HttpKernel\Exception\NotFoundHttpException" log-level="info" status-code="0" />
<framework:exception name="Symfony\Component\HttpKernel\Exception\ConflictHttpException" log-level="info" status-code="0" />
<framework:exception name="Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException" log-level="null" status-code="500" />
</framework:exceptions>
</framework:config>
</container>
9 changes: 9 additions & 0 deletions Tests/DependencyInjection/Fixtures/yml/exceptions.yml
Expand Up @@ -3,3 +3,12 @@ framework:
Symfony\Component\HttpKernel\Exception\BadRequestHttpException:
log_level: info
status_code: 422
Symfony\Component\HttpKernel\Exception\NotFoundHttpException:
log_level: info
status_code: null
Symfony\Component\HttpKernel\Exception\ConflictHttpException:
log_level: info
status_code: 0
Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException:
log_level: null
status_code: 500
12 changes: 12 additions & 0 deletions Tests/DependencyInjection/FrameworkExtensionTest.php
Expand Up @@ -552,6 +552,18 @@ public function testExceptionsConfig()
'log_level' => 'info',
'status_code' => 422,
],
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class => [
'log_level' => 'info',
'status_code' => null,
],
\Symfony\Component\HttpKernel\Exception\ConflictHttpException::class => [
'log_level' => 'info',
'status_code' => null,
],
\Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException::class => [
'log_level' => null,
'status_code' => 500,
],
], $container->getDefinition('exception_listener')->getArgument(3));
}

Expand Down

0 comments on commit 4b9e1e4

Please sign in to comment.