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

show php errors when using debug component

Am using Debug component which works very well.

However, as far as I understand the component is meant to debug the framework and not errors such as Fatal errors.

I can't figure out how I will keep the debug component but still display such errors not caught by the debug component.



85.5k

https://php.net/manual/en/function.set-error-handler.php


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

    $logger = Phalcon\Di\FactoryDefault\Cli::getDefault()->getShared("logger");

    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting
        return;
    }

    $msg = "[$errno] $errstr ; \n on line $errline in file $errfile \n";

    switch ($errno) {
        case E_USER_ERROR:
            $logger->critical("fatal error: ". $msg);
            exit(1);
            break;

        case E_USER_WARNING:
            $logger->warning("warning error: ". $msg);
            break;

        case E_USER_NOTICE:
            $logger->notice("notice error: ". $msg);
            break;

        default:
            $logger->alert( "Unknown error type: ".$msg);
            break;
    }

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

set_error_handler("myErrorHandler");
edited Jan '17

I got it working;

Before I was using the following in order to display errors but ended in a white screen.

    error_reporting(E_ALL);

After a while I decided to use the following and all my errors got displayed as I expected.

ini_set('display_errors', '1');

I have no deep explanation but init_set() Sets the value of the given configuration option. The configuration option will keep this new value during the script's execution, and will be restored at the script's ending. While error_reporting() function does not change the server configuration options but sets the error_reporting directive at runtime

I hope in the next Phalcon release we can improve the debug component such that if you want to display errors not related to the framework you just pass some argument settings when constructing the object.

In PHP7, there's a new Exception class which can handle also fatal errors. I'm busy now but I'll post it later.

Also for PHP 5.x I'm using register_shutdown_function() to catch fatal errors etc.