Description
Symfony version(s) affected
6.4.10
Description
- Symfony:
6.4.10
- Monolog:
3.7.0
I stumbled on this issue after upgrading from Symfony 5 to 6. When a HttpException is thrown inside the callback of a StreamedResponse, it is ignored by excluded_http_codes
and logged anyway.
This happens because $this->requestStack->getMainRequest()
returns null
in https://github.com/symfony/symfony/blob/6.4/src/Symfony/Bridge/Monolog/Handler/FingersCrossed/HttpCodeActivationStrategy.php#L53
This is a consequence of the stack->pop()
done in https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/HttpKernel/HttpKernel.php#L103 as soon as the callback ran.
How to reproduce
- Setup a
fingers_crossed
monolog handler withexcluded_http_codes
main:
type: fingers_crossed
action_level: warning
handler: json
excluded_http_codes: [400, 401, 403, 404, 405, 429]
buffer_size: 50
json:
type: rotating_file
path: "%kernel.logs_dir%/%kernel.environment%.log"
formatter: 'monolog.formatter.json'
level: debug
max_files: 2
- Create a simple controller that returns a StreamedResponse:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Attribute\Route;
class HomeController extends AbstractController
{
#[Route('/home', name: 'app_home')]
public function index(): StreamedResponse
{
$response = new StreamedResponse();
$response->setCallback(function () {
throw new NotFoundHttpException('This is a test exception');
});
return $response;
}
}
-
Go to
/home
and notice that the 404 error is logged -
Move
throw new NotFoundHttpException('This is a test exception');
before thesetCallback()
function and notice that the 404 error is not logged (as expected)
Possible Solution
No response
Additional Context
No response