How to store the error that Ci_exception provides

Asked

Viewed 103 times

0

Hello, I would like to capture PHP errors in codeigniter and store them in the database, or at least display them more smoothly to the user, in the error_php.php file all the details I need are displayed, up to the line of code where the error occurred, but I would like to get this information in the controller to be able to store.

<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 with try catch but it didn’t work, I tried to play for flashdata and then use, but it’s not correct and it still didn’t work either.

  • How did you try to capture Exception? in production environment this giant msg some (you need to set this up).

  • Hi, so I tried yes, but actually I don’t want it to disappear no, I want to be able to store the error in the database. or at least work it in a more friendly way. it seems that when Exception is even possible, then it redirects to the erro_exception view, but in this case it sends to erro_php, I think there is no Exception for these errors (I think)

1 answer

0

It is possible to do what you want, but this includes changing the core of Codeigniter. Is that Codeigniter defines a custom PHP error management when calling the function below in the file /system/core/CodeIgniter.php:

set_error_handler('_error_handler');

And the treatment of exceptions goes to function _error_handler( ) in the archive /system/core/Commom.php. Note that it is in this function 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
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.