cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG] Missing a method onUncaughtLowSeverity in Phalcon\Debug

Open beermeat opened this issue 13 years ago • 10 comments

$debug = new \Phalcon\Debug();
$debug->listen(true, true);

an error:

Warning: set_exception_handler() expects the argument (Phalcon\Debug::onUncaughtLowSeverity) to be a valid callback in ...

And why "set_exception_handler"? Maybe "set_error_handler"?

1.2.1

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

beermeat avatar Jul 12 '13 07:07 beermeat

pleeease fix this

beermeat avatar Aug 03 '13 19:08 beermeat

Please try 1.2.2 or 1.3.0

ghost avatar Aug 04 '13 05:08 ghost

https://github.com/phalcon/cphalcon/blob/1.3.0/ext/debug.c

PHP_METHOD(Phalcon_Debug, listenLowSeverity){
/*
    zval *handler;

    PHALCON_MM_GROW();

    PHALCON_INIT_VAR(handler);
    array_init_size(handler, 2);
    phalcon_array_append(&handler, this_ptr, PH_SEPARATE);
    add_next_index_stringl(handler, SL("onUncaughtLowSeverity"), 1);
    phalcon_call_func_p1_noret("set_exception_handler", handler);
*/
    RETURN_THIS();
}

nice way :D but it would be better to implement a functional.

beermeat avatar Aug 04 '13 10:08 beermeat

I don't know what is meant to be here therefore I just commented out the code :P

ghost avatar Aug 04 '13 13:08 ghost

here's the solution in PHP, may still be useful to someone (+bonus: catch fatal errors)

<?php

namespace Mod;

class Debug extends \Phalcon\Debug
{
    public function listenLowSeverity()
    {
        set_error_handler(array($this, 'onUncaughtLowSeverity'));

        // catch fatal errors
        register_shutdown_function(function()
        {
            $error = error_get_last();

            if ($error && ($error['type'] == E_ERROR)) {
                $this->onUncaughtLowSeverity($error['type'], $error['message'], $error['file'], $error['line']);
            }
        });

        return $this;
    }

    public function onUncaughtLowSeverity($errNo, $errorStr, $errorFile, $errorLine)
    {
        /**
         * Cancel the output buffer if active
         */
        if (ob_get_level() > 0) {
            ob_end_clean();
        }

        /**
         * Avoid that multiple errors being showed
         */
        if (self::$_isActive) {
            echo $errorStr;
        }

        /**
         * Globally block the debug component to avoid other errors must be shown
         */
        self::$_isActive = true;

        switch ($errNo) {
            case E_ERROR:
                $errorTitle = 'Fatal error';
                break;
            case E_WARNING:
                $errorTitle = 'Warning';
                break;
            case E_PARSE:
                $errorTitle = 'Parse error';
                break;
            case E_NOTICE:
                $errorTitle = 'Notice';
                break;
            case E_CORE_ERROR:
                $errorTitle = 'Core error';
                break;
            case E_CORE_WARNING:
                $errorTitle = 'Core warning';
                break;
            case E_COMPILE_ERROR:
                $errorTitle = 'Compile error';
                break;
            case E_COMPILE_WARNING:
                $errorTitle = 'Compile warning';
                break;
            case E_USER_ERROR:
                $errorTitle = 'User error';
                break;
            case E_USER_WARNING:
                $errorTitle = 'User warning';
                break;
            case E_USER_NOTICE:
                $errorTitle = 'User notice';
                break;
            case E_STRICT:
                $errorTitle = 'Strict';
                break;
            case E_RECOVERABLE_ERROR:
                $errorTitle = 'Fatal error';
                break;
            case E_DEPRECATED:
                $errorTitle = 'Deprecated';
                break;
            case E_USER_DEPRECATED:
                $errorTitle = 'User deprecated';
                break;
            default:
                $errorTitle = 'Unknown error';
        }

        /**
         * Escape the error's message avoiding possible XSS injections?
         */
        $errorStr = htmlspecialchars($errorStr, ENT_QUOTES);

        /**
         * Use the error info as document's title
         */
        $html  = '<html><head><title>PHP ' . $errorTitle . ': ' . $errorStr . '</title>' . $this->getCssSources() . '</head><body>';

        /**
         * Get the version link
         */
        $html .= $this->getVersion();

        /**
         * Main error info
         */
        $html .= '<div align="center"><div class="error-main">';
        $html .= '<h1>PHP ' . $errorTitle .  ': ' . $errorStr . '</h1>';
        $html .= '<span class="error-file">' . $errorFile . ' (' . $errorLine . ')</span>';
        $html .= '</div>';

        /**
         * Check if the developer wants to show the backtrace or not
         */
        if ($this->_showBackTrace) {
            /**
             * Create the tabs in the page
             */
            $html .= '<div class="error-info"><div id="tabs"><ul>';
            $html .= '<li><a href="#error-tabs-1">Backtrace</a></li>';
            $html .= '<li><a href="#error-tabs-2">Request</a></li>';
            $html .= '<li><a href="#error-tabs-3">Server</a></li>';
            $html .= '<li><a href="#error-tabs-4">Included Files</a></li>';
            $html .= '<li><a href="#error-tabs-5">Memory</a></li>';

            if (is_array($this->_data)) {
                $html .= '<li><a href="#error-tabs-6">Variables</a></li>';
            }

            $html .= '</ul>';

            /**
             * Print backtrace
             */
            $html .= '<div id="error-tabs-1"><table cellspacing="0" align="center" width="100%">';

            if ($errNo == E_ERROR) {
                $html .= $this->showTraceItem(0, array('file' => $errorFile, 'line' => $errorLine, 'function' => 'Fatal error:'));
            } else {
                $trace = debug_backtrace();

                foreach ($trace as $key => $line) {
                    if ($key === 0) {
                        continue;
                    }

                    /**
                     * Every line in the trace is rendered using 'showTraceItem'
                     */
                    $html .= $this->showTraceItem($key, $line);
                }
            }


            $html .= '</table></div>';

            /**
             * Print _REQUEST superglobal
             */
            $html .= '<div id="error-tabs-2"><table cellspacing="0" align="center" class="superglobal-detail">';
            $html .= '<tr><th>Key</th><th>Value</th></tr>';

            foreach ($_REQUEST as $key => $value) {
                $html .= '<tr><td class="key">' . $key . '</td><td>' . htmlspecialchars($value, ENT_QUOTES) . '</td></tr>';
            }

            $html .= '</table></div>';

            /**
             * Print _SERVER superglobal
             */
            $html .= '<div id="error-tabs-3"><table cellspacing="0" align="center" class="superglobal-detail">';
            $html .= '<tr><th>Key</th><th>Value</th></tr>';

            foreach ($_SERVER as $key => $value) {
                $html .= '<tr><td class="key">' . $key . '</td><td>' . htmlspecialchars($value, ENT_QUOTES) . '</td></tr>';
            }

            $html .= '</table></div>';

            /**
             * Show included files
             */
            $files = get_included_files();

            $html .= '<div id="error-tabs-4"><table cellspacing="0" align="center" class="superglobal-detail">';
            $html .= '<tr><th>#</th><th>Path</th></tr>';

            foreach ($files as $key => $value) {
                $html .= '<tr><td>' . $key . '</td><td>' . $value . '</td></tr>';
            }

            $html .= '</table></div>';

            /**
             * Memory usage
             */
            $memory = memory_get_usage(true);

            $html .= '<div id="error-tabs-5"><table cellspacing="0" align="center" class="superglobal-detail">';
            $html .= '<tr><th colspan=\"2\">Memory</th></tr><tr><td>Usage</td><td>' . $memory . '</td></tr>';

            $html .= '</table></div>';

            /**
             * Print extra variables passed to the component
             */
            if (is_array($this->_data)) {
                $html .= '<div id="error-tabs-6"><table cellspacing="0" align="center" class="superglobal-detail">';
                $html .= '<tr><th>Key</th><th>Value</th></tr>';

                foreach ($this->_data as $key => $value) {

                    $html .= '<tr><td class="key">' . $key . '</td><td>' . $this->_getVarDump($value[0]) . '</td></tr>';
                }

                $html .= '</table></div>';
            }

            $html .= '</div>';
        }

        /**
         * Get Javascript sources
         */
        $html .= $this->getJsSources() . '</div></body></html>';

        /**
         * Print the HTML, @TODO, add an option to store the html
         */
        echo $html;

        /**
         * Unlock the exception renderer
         */
        self::$_isActive = false;

        exit;
    }
}

beermeat avatar Aug 05 '13 17:08 beermeat

As far as I understand the logic, onLowSeverity was meant for non-fatal errors (E_NOTICE, E_USER_NOTICE, E_WARNING, E_USER_WARNING, …) and hence low severity.

If we get a fatal error, we just display all information we have and abort the application.

But I am not sure what to do on a notice / warning. Your solution will abort the application after the first warning or notice, do you think this is the best way to go?

@phalcon Need your feedback

ghost avatar Aug 05 '13 17:08 ghost

yes, for fatal errors will be better to add a new method and a new option for the listen()

I think that is the best way. Because in any case, it will only be used on a dev server. А similar behavior in Yii

beermeat avatar Aug 05 '13 18:08 beermeat

@sjinks onLowSeverity was meant to show any warning or notices as an exception making the framework more object-oriented and strict. The implementation in PHP in this thread is OK, however parsing and fatal errors cannot be catched since it just only change the default error handler by a Phalcon one.

ghost avatar Aug 05 '13 18:08 ghost

The method still doesn't exist in 2.0. https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/debug.zep#L110

Green-Cat avatar Apr 16 '15 15:04 Green-Cat

Fixed in 2.0.x

andresgutierrez avatar May 19 '15 16:05 andresgutierrez