Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display fatal/notice errors in browser

Tags:

hhvm

Well I just started with hhvm/hack. But I wanted to display the errors to the browser but didnot got it working.

I set the ini settings as followed

error_reporting(E_ALL);
ini_set('display_errors', '1');

According to a var_dump of ini_get the values were set to

string(5) "32767"
string(1) "1"

But when I did

<?hh

error_reporting(E_ALL);
ini_set('display_errors', '1');

throw new InvalidArgumentException('test');

When I visited the page via de browser I would just get a white screen and a 500 http header. So no explaintion of the fatal/exception error.

Would I however go via the terminal hhvm index.php It would show;

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'test' in /var/www/public/index.php:3
Stack trace:
#0 {main}

So now the question. How does it come I don't get any messages in the browser, but do in the cli? And the second one is, how do I get it working in the browser to show messages.

I came across this question and this one. But the first, well is the same, but unanswerd. And the second is saying something about static checking. IE that an int is given etc, at least that is what I think he means.

Other question I came by, which looked like mine, but again unanswered.

And according to the docs it should work I guess.

Reading more on the docs I came accross

Although display_errors may be set at runtime (with function ini_set()), it won't have any affect if the script has fatal errors. This is because the desired runtime action does not get executed.

So I thought well an Exception is a fatal error so I just did $test = $bar + 1;

This let in the cli to

Notice: Undefined variable: bar in /var/www/public/index.php on line 8
int(1)

Again in the browser

int(1) 

So I don't get notices either.

When I restart hhvm service hhvm restart I also get the error message Log file not specified under daemon mode. don't know if it has anything to do with it

like image 499
MKroeders Avatar asked Jul 02 '14 06:07

MKroeders


People also ask

How can I see PHP errors in my browser?

The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

What is fatal error in website?

A fatal error occurs when an application tries to access invalid data or code, an illegal action is attempted or an infinity condition is met. The program shuts down and returns the user to the operating system.

Which of the following PHP directive is used to display all the errors except notices?

error_reporting: It displays all level errors except E-NOTICE, E-STRICT, and E_DEPRECATED level errors. display_errors: By default, the value of display_errors is off.


2 Answers

Well it took me some searching but finally found the answer, see this github post. It can't however display fatal errors. But notices are displayed

So I wrote my own error handler; it is not complete but does the job for now.

Warning, no check for ini display_errors

set_error_handler(function ($errorNumber, $message, $errfile, $errline) {
    switch ($errorNumber) {
        case E_ERROR :
            $errorLevel = 'Error';
            break;

        case E_WARNING :
            $errorLevel = 'Warning';
            break;

        case E_NOTICE :
            $errorLevel = 'Notice';
            break;

        default :
            $errorLevel = 'Undefined';
    }

    echo '<br/><b>' . $errorLevel . '</b>: ' . $message . ' in <b>'.$errfile . '</b> on line <b>' . $errline . '</b><br/>';
});
like image 142
MKroeders Avatar answered Oct 08 '22 21:10

MKroeders


It's possible to fall back to php when hhvm fails. Pretty simple to do that - https://bjornjohansen.no/hhvm-with-fallback-to-php. I understand you can attach php fallback per error code (like 404). So isn't it possible to fall back to php server that shows a page which loads the hhvm error log?

like image 39
Hannes Kirsman Avatar answered Oct 08 '22 22:10

Hannes Kirsman