D
D
daMage2015-03-27 12:42:51
PHP
daMage, 2015-03-27 12:42:51

How to remove fatal error output in php?

I decided to work on the error system, set up exception logging and throw a handler on E_* errors. In the case of non-fatal errors, everything is fine, but to catch errors like E_ERROR | E_PARSE does not work, but I would like to. In general, I found a suitable article on Habré, copied all the code and pasted it at the very top of the document. Removed ";" in one of the lines, but the Parse error remains as it was. Page Code

/**
 * Обработчик ошибок
 * @param int $errno уровень ошибки
 * @param string $errstr сообщение об ошибке
 * @param string $errfile имя файла, в котором произошла ошибка
 * @param int $errline номер строки, в которой произошла ошибка
 * @return boolean
 */
function error_handler($errno, $errstr, $errfile, $errline)
{
    // если ошибка попадает в отчет (при использовании оператора "@" error_reporting() вернет 0)
    if (error_reporting() & $errno)
    {
        $errors = array(
            E_ERROR => 'E_ERROR',
            E_WARNING => 'E_WARNING',
            E_PARSE => 'E_PARSE',
            E_NOTICE => 'E_NOTICE',
            E_CORE_ERROR => 'E_CORE_ERROR',
            E_CORE_WARNING => 'E_CORE_WARNING',
            E_COMPILE_ERROR => 'E_COMPILE_ERROR',
            E_COMPILE_WARNING => 'E_COMPILE_WARNING',
            E_USER_ERROR => 'E_USER_ERROR',
            E_USER_WARNING => 'E_USER_WARNING',
            E_USER_NOTICE => 'E_USER_NOTICE',
            E_STRICT => 'E_STRICT',
            E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
            E_DEPRECATED => 'E_DEPRECATED',
            E_USER_DEPRECATED => 'E_USER_DEPRECATED',
        );

        // выводим свое сообщение об ошибке
        echo "<b>{$errors[$errno]}</b>[$errno] $errstr ($errfile на $errline строке)<br />\n";
    }

    // не запускаем внутренний обработчик ошибок PHP
    return TRUE;
}

/**
 * Функция перехвата фатальных ошибок
 */
function fatal_error_handler()
{
    // если была ошибка и она фатальна
    if ($error = error_get_last() AND $error['type'] & ( E_ERROR | E_PARSE | E_COMPILE_ERROR | E_CORE_ERROR))
    {
        // очищаем буффер (не выводим стандартное сообщение об ошибке)
        ob_end_clean();
        // запускаем обработчик ошибок
        error_handler($error['type'], $error['message'], $error['file'], $error['line']);
    }
    else
    {
        // отправка (вывод) буфера и его отключение
        ob_end_flush();
    }
}

// определеяем уровень протоколирования ошибок
error_reporting(E_ALL | E_STRICT);
// определяем режим вывода ошибок
ini_set('display_errors', 'On');
// включаем буфферизацию вывода (вывод скрипта сохраняется во внутреннем буфере)
ob_start();
// устанавливаем пользовательский обработчик ошибок
set_error_handler("error_handler");
// регистрируем функцию, которая выполняется после завершения работы скрипта (например, после фатальной ошибки)
register_shutdown_function('fatal_error_handler');

echo 1

The site is local, there are rights to change the ini-file. PHP version 5.5.9

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
FanatPHP, 2015-03-27
@FanatPHP

How to remove fatal error output in php?

?

A
Andrey Mokhov, 2015-03-27
@mokhovcom

The link you provided says in black and white:
Ability to catch an error by a function defined in set_error_handler():

  • Intercepted (non-fatal and mixed)
    E_USER_ERROR, E_RECOVERABLE_ERROR, E_WARNING, E_NOTICE, E_USER_WARNING, E_USER_NOTICE, E_STRICT, E_DEPRECATED, E_USER_DEPRECATED.
    E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING.
    I highlighted in bold...

V
Valery Ryaboshapko, 2015-03-27
@valerium

A number of errors cannot be caught from inside the script, such as E_PARSE (parsing error) and E_ERROR (runtime error), plus errors from PHP itself.
However, they do not need to be caught inside the script, there are unit and integration tests for them, as well as stable builds of PHP.

D
Denis, 2015-03-27
@prototype_denis

Everything is much simpler...
This file must be "perfect" and not contain any errors, then everything will work fine.
Do you want mistakes? Connect the file with errors, but do not touch this one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question