hhvm-fastcgi + nginx how to make it display fatal errors in the browser

痴心易碎 提交于 2019-11-30 17:30:19

You need to enable the configuration hhvm.server.implicit_flush in your php.ini, then you can send a response body in case of fatal errors. To be able to catch fatal errors with an error handler, you should also enable hhvm.error_handling.call_user_handler_on_fatals.

For details, refer to the github issue on hhvm.

Use a custom error handler to handle any type of error exactly the way you want it to. Taken almost directly from example #1 in the link...

function myErrorHandler($errno, $errstr, $errfile, $errline)
{

    switch ($errno) {
    case E_USER_ERROR:
        echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
        echo "  Fatal error on line $errline in file $errfile";
        echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
        echo "Aborting...<br />\n";
        exit(1);
        break;

   case E_USER_WARNING:
        echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
        break;

    case E_USER_NOTICE:
        echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
        break;

    default:
        echo "Unknown error type: [$errno] $errstr<br />\n";
        break;
    }

    /* Don't execute PHP internal error handler */
    return true;
}

set_error_handler("myErrorHandler");

For this approach to work, you have to set the error handler as early as possible in your code.

As you might have noticed I left one bit of code out, namely the one that checks if the error type is configured to be reported in your php/hhvm configuration. With the code above the errors will show regardless of your php/hhvm configuration. (so you probably want to log instead of echo errors in production environment hint)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!