How to store the error that CI_Exception provides

0
Hello, I would like to capture the PHP errors in codeigniter and store in the database, or at least display a smoother way to the user, in the file error_php.php are displayed all the details I need, up to the line of the code where the error but I'd like to get this information in the controller so I can store it.

<h4>A PHP Error was encountered</h4>

<p>Severity: <?php echo $severity; ?></p>
<p>Message:  <?php echo $message; ?></p>
<p>Filename: <?php echo $filepath; ?></p>
<p>Line Number: <?php echo $line; ?></p>

I tried Try catch but it did not work, I tried to play to flashdata and then use, but it is not correct and even then it did not work either.

    
asked by anonymous 31.07.2017 / 23:58

1 answer

0

You can do what you want, but this includes changing the CodeIgniter core. CodeIgniter defines a custom PHP error handling when calling the function below in the /system/core/CodeIgniter.php file:

set_error_handler('_error_handler');

And exception handling passes to the _error_handler( ) function in the /system/core/Commom.php file. Note that this is the scope of the variables you want to use:

function _error_handler($severity, $message, $filepath, $line) {
    $is_error = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $severity) === $severity);

    if ($is_error)
        set_status_header(500);

    if (($severity & error_reporting()) !== $severity)
        return;

    $_error =& load_class('Exceptions', 'core');
    $_error->log_exception($severity, $message, $filepath, $line);

    if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors')))
        // AQUI ESTÃO AS VARIÁVEIS PRONTAS QUE VOCÊ DESEJA
        // O CODEIGNITER ESTÁ PRONTO PARA CHAMAR A VIEW

        // INSIRA DADOS NO BANCO AQUI, DE ACORDO COM USUÁRIO LOGADO, EXCEÇÃO, ETC...
        // $severity, $message, $filepath, $line !!!            

        $_error->show_php_error($severity, $message, $filepath, $line);

    if ($is_error)
        exit(1); // EXIT_ERROR
}
    
08.08.2017 / 15:35