5

I am deploying my Symfony2 application, but I am getting the following error:

FatalErrorException in classes.php line 0:
Error: Method Symfony\Component\HttpFoundation\Request::__toString() must not throw an exception

Apache is slightly more descriptive, stating something about Monolog:

 PHP Fatal error:  Method Symfony\\Component\\HttpFoundation\\Request::__toString() must not throw an exception in /my/path/vendor/monolog/monolog/src/Monolog/Formatter/NormalizerFormatter.php on line 0

It is also the only thing It is not clear to me how this is happening. Dev-environment on my local machine is running fine.

I have tried to clear the prod cache, the composer cache and restarted apache service. I also did a fresh "composer install" after clearing its cache.

Anyone has an idea how this can be solved? Am running Symfony v2.7.4.

xfscrypt
  • 16
  • 5
  • 28
  • 59
  • I'm at work and have just hit EXACTLY the same issue ... for me it happened when I added a trusted_host to my config ... Let me know if you get to the bottom of it and I'll do the same for you – Ragdata Sep 21 '15 at 07:07
  • 2
    [At]Ragdata: I just fixed it. In one of the controllers I had an annotation: "* @param Request $request", but the $request was actually not a parameter anymore, i forgot to remove the annotation. – xfscrypt Sep 21 '15 at 07:21
  • Damnit ... good news - but doesn't quite get there for me. Does have to do with an empty request hitting the logger though I think. I've traced it to Monolog\Formatter\NormalizeFormatter.php ... I'll see what else I can find on it – Ragdata Sep 21 '15 at 07:30
  • another thing I have changed, I didnt check if this also affected it, is the to correct the list of trusted hosts. finaly I made sure the monolog configuration is the same as when it came whith the initial install of symfony2 – xfscrypt Sep 21 '15 at 07:43
  • Crap, really? 'Cause I've added a whole BUNCH of custom handlers (none of which appeared to be hit in the stack trace by the way) ... – Ragdata Sep 21 '15 at 07:50

1 Answers1

0

You should to check your $_COOKIE or $_SESSION for example; The trouble is you could have array value in $_COOKIE, so on your debug environment you could fall into the following situation:

Debug::enable() //see, i don't exclude E_NOTICE here
$request = Request::createFromGlobals();
echo $request->__toString();

//so, from now you've got your Exception because of
//Debug package converts E_NOTICE to Fatal
//which turns into Exception 

So if you want to fix this trouble you need to:

  1. find the real reason of exception triggering (notice, deprecated, strict message, etc)
  2. exclude it from Debug: Debug::enable(~E_NOTICE);

OR just remove (string)$request from your code if possible;

see Request code:

//part of __toString() method
foreach ($this->cookies as $k => $v) {
    $cookies[] = $k.'='.$v;
}
alexglue
  • 1,292
  • 12
  • 18