8000 [FrameworkBundle] Allow to specify `null` for exception mapping configuration values by andrew-demb · Pull Request #46956 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Allow to specify null for exception mapping configuration values #46956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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,
],
],
]);
Original file line number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,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
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ class ErrorListener implements EventSubscriberInterface
protected $controller;
protected $logger;
protected $debug;
/**
* @var array<class-string, array{log_level: string|null, status_code: int<100,599>|null}>
*/
protected $exceptionsMapping;

/**
* @param array<class-string, array{log_level: string|null, status_code: int<100,599>|null}> $exceptionsMapping
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it welcome to explicitly specify typing for mapping structure in Symfony codebase for now?

*/
public function __construct($controller, LoggerInterface $logger = null, bool $debug = false, array $exceptionsMapping = [])
{
$this->controller = $controller;
Expand Down
0