Skip to content

excluded_http_codes not working as intended for http errors thrown inside the callback of a StreamedResponse #57902

Closed
@mathieuimbert

Description

@mathieuimbert

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

  1. Setup a fingers_crossed monolog handler with excluded_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
  1. 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;
    }
}
  1. Go to /home and notice that the 404 error is logged

  2. Move throw new NotFoundHttpException('This is a test exception'); before the setCallback() function and notice that the 404 error is not logged (as expected)

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions