0

I've created an exceptionListener which is catches exceptions. It's registered:

  App\Listener\ExceptionListener:
    tags:
      - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

also got the logic to handle throwables as well. In this example, i'd like to create a simple redirectionResponse:

$event->setResponse(new RedirectResponse('/error.html', 302, []));

ofc the /error.html is available, I've ran from the URL bar.

And yet if I catching the exception, after the redirection, I see app.CRITICAL log entry right after the redirection URL:

app.CRITICAL: Failed to start the session because headers have already been sent by "/home/PROJECT/vendor/symfony/http-foundation/Response.php" at line 368.

so the error log refers to the response that I've set up for $event->setResponse(...) however I didn't touch any header specific events e.g. header() setCookie(), etc.

Is there any way to trace back where the first header was sent?

Roland
  • 183
  • 4
  • 21
  • Symfony does not actually send anything until the very end when the $request->send() method is called. Assuming your code is not intentionally echo'ing something then check for things like whitespace before or after your – Cerad Feb 25 '21 at 22:29
  • @Cerad if I skip the exceptionListener (not registering in the services.yaml) then everything seems fine. No "header already sent" rows in the logs. Tried to add a new Response with content only but the issue the same => header already sent error. – Roland Feb 26 '21 at 05:06
  • You can try using [`headers_sent($filename, $line)`](https://www.php.net/manual/en/function.headers-sent.php) to see if it can identify where the headers are sent. If your code doesn't do it, chances are you accidentally sent them. For instance, having a whitespace before an opening ` – dbrumann Feb 26 '21 at 08:42
  • @dbrumann i got false everywhere i've checked `headers_sent()` also checked the files for any opening php tag - nothing – Roland Feb 26 '21 at 13:50
  • In that case I recommend using something like postman to send the request and analyze the response. Hopefully at the very least you can see which headers are sent, which might make it easier to see where it might happen. – dbrumann Feb 26 '21 at 20:19
  • @dbrumann thank you i'm going to check that software – Roland Feb 27 '21 at 06:38

0 Answers0