We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Handling fatal errors in Phalcon 1.3.4 and Nginx

Hi,

Not long ago I noticed that my app doesn't log fatal errors properly because sometihng went wrong. It seemed like fatal error(wrong parsing) caused error controller not to boot up so I replaced order of few booting methods an stuff worked.

But the thing is that it works only on apache and nginx still produces default 500 error. I've edited php-fpm and nginx configs, added various flags but still no luck.

It crashes whern I try to call distpatch function in my errors distpacher and php error output is:

Phalcon\Mvc\Dispatcher->_throwDispatchException

Entire error for this event looks like this:

2016/03/02 12:09:20 [error] 1973#0: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Call to a member function format() on boolean in /www/web/public/app/Web/Controller/TestController.php on line 59
PHP message: PHP Stack trace:
PHP message: PHP   1. {main}() /www/web/public/public/index.php:0
PHP message: PHP   2. Phalcon\Mvc\Application->handle() /www/web/public/public/index.php:10
PHP message: PHP   3. Phalcon\Dispatcher->dispatch() /www/web/public/public/index.php:10
PHP message: PHP   4. MyApp\Web\Controller\TestController->fatalAction() /www/web/public/public/index.php:10
PHP message: PHP Fatal error:  Uncaught exception 'Phalcon\Mvc\Dispatcher\Exception' with message 'MyApp\Web\Controller\ErrorController handler class cannot be loaded' in /www/web/public/app/Application.php:363
Stack trace:
0 [internal function]: Phalcon\Mvc\Dispatcher->_throwDispatchException('MyApp\\Web\\Con...', 2)
1 /www/web/public/app/Application.php(363): Phalcon\Dispatcher->dispatch()
2 /www/web/public/app/Application.php(102): MyApp\Application->handleError(Array)
3 [internal function]: MyApp\Application->MyApp\{closure}()
4 {main}
  thrown in /www/web/public/app/Application.php on line 363
PHP message: PHP Stack trace:
PHP message: PHP   1. {main}() /www/web/public/public/index.php:0
PHP message: PHP   2. Phalcon\Mvc\Application->handle() /www/web/public/public/index.php:10
PHP message: PHP   3. Phalcon\Dispatcher->dispatch() /www/web/public/public/index.php:10
PHP message: PHP   4. MyApp\Web\Controller\TestController->fatalAction() /www/web/public/public/index.php:10" while reading response header from upstream, client: 192.168.30.1, server: app.loc, request: "GET /test/fatal HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "app.loc"

Has anyone encountered this kind of problem where fatal error on (nginx + php-fpm) caused you application to crash without calling some kind of error controller or some other logging mechanism? And how you solved that issue?

P.S I know i can try cactch and write it to log by brue force it but I really want to understand why this happend and why apache behaves diferently from nginx when handling fatal errors in this case.

First thing, you should upgrade to 2.0 branch.

Ok I know I should, but existing codebase is already there so as this error. This is pretty common error as I could see over the internet so I'll guess it hasn't been solved yet.

Root cause of your issue is PHP and fatal error:

PHP Fatal error: Call to a member function format() on boolean in /www/web/public/app/Web/Controller/TestController.php on line 59

You should check that first.

Nginx returns 500 int. server error HTTP status / error screen in such cases, since it is in a bad position to judge what exactly has happend other than fatal error recieved by an upstream application server (back-end).

The only difference on Apache this error would display directly in a browser / app (in case of mod_php).

Yeah, I've made that error on purpose for so I could investigate this issue further.

The thing is that I cant catch error in Phalcon to handle it somewhere in logs on Nginx while It's possible to do so in Apache. I guess it must be something with Nginx config.

Root cause of your issue is PHP and fatal error:

PHP Fatal error: Call to a member function format() on boolean in /www/web/public/app/Web/Controller/TestController.php on line 59

You should check that first.

Nginx returns 500 int. server error HTTP status / error screen in such cases, since it is in a bad position to judge what exactly has happend other than fatal error recieved by an upstream application server (back-end).

The only difference on Apache this error would display directly in a browser / app (in case of mod_php).